简体   繁体   English

结构c中的多维数组

[英]multidimensional array in struct c

I've been trying this for a long time. 我已经尝试了很长时间了。 Everything seems to work but i get a lot of warnings and just want to program it properly. 一切似乎正常,但我收到很多警告,只想对它进行正确的编程。 The problem is the following: I'm making a game engine. 问题如下:我正在制作一个游戏引擎。 I provide images in 3d arrays, where the first two indices are the position and the third is the colour. 我提供3d阵列中的图像,其中前两个索引是位置,第三个索引是颜色。 So a simple 4x4 image would be: 因此,一个简单的4x4图像将是:

static const uint8_t font_image[2][2][3] = 
{
    {{255,255,255},{255,255,255}},
    {{255,255,255},{255,255,255}}
};

Now every entity struct has a sprite struct. 现在,每个实体结构都有一个精灵结构。 All i want is to have a reference to the 3 dimensional array in the struct. 我想要的是在结构中引用3维数组。

struct Sprite {
    uint32_t sizeX;
    uint32_t sizeY;
    uint8_t * data;
};

Every image can his its own width and height, so i can't make it 每个图像都可以拥有自己的宽度和高度,所以我做不到

uint8_t [2][2][3] data;

I keep getting this warning: 我不断收到此警告:

Message 3 expected 'const uint8_t *' but argument is of type 'const uint8_t (*)[10][4][3]' C:\\Users\\Elmar\\Documents\\GitHub\\EVA-OS\\EVA-OS\\Libs\\Game\\sprite.h 28 10 EVA-OS 消息3预期为'const uint8_t *',但参数的类型为'const uint8_t(*)[10] [4] [3]'C:\\ Users \\ Elmar \\ Documents \\ GitHub \\ EVA-OS \\ EVA-OS \\ Libs \\ Game \\ sprite.h 28 10 EVA-OS

where this is the function to make a new sprite: 这是制作新精灵的功能:

Sprite * sprite_new(uint32_t width, uint32_t height, const uint8_t * image)
{
    Sprite * sprite = malloc(sizeof(Sprite));
    sprite->data = image;
    sprite->sizeX = width;
    sprite->sizeY = height;
    return sprite;
}

Thanks in advance! 提前致谢! :) :)

From your error message, it sounds like you're doing this somewhere else in your code: 从错误消息中,听起来您正在代码中的其他地方执行此操作:

uint8_t image[10][4][3] = { ... whatever ... };

Sprite *sprite = sprite_new(10, 4, image);

The problem with that is that the compiler considers the data type of image to be const uint8_t (*)[10][4][3] , not uint8_t * . 这样做的问题是,编译器认为image的数据类型为const uint8_t (*)[10][4][3] ,而不是uint8_t * You can make it work with a cast: 您可以通过强制转换使其工作:

Sprite *sprite = sprite_new(10, 4, (uint8_t *)image);

As for how you declare your Sprite struct, user2357112 is correct. 至于您如何声明Sprite结构,user2357112是正确的。 There's really no easy way to declare variable length arrays and pass them into functions in C with their size information preserved. 实际上,没有简单的方法来声明可变长度数组并将它们传递给C并保留其大小信息的函数。 C is kind of dumb in that regard. 在这方面,C有点愚蠢。 You always need to pass the size information as one or more separate parameters. 您始终需要将大小信息作为一个或多个单独的参数传递。

Before 之前

sprite->data = image;

are you allocating space for the data? 您是否在为数据分配空间?

I would assume you would have to allocate space for struct Sprite but that will only contain space for a pointer. 我假定您将不得不为struct Sprite分配空间,但是它将只包含一个指针空间。 Then you will need to allocate space for a regoin and have data point to it. 然后,您将需要为重新分配分配空间,并有数据指向它。 Finallly, you can memcopy stuff in that space ... Finallly,您可以memcopy在这个空间的东西...

Sprite * sprite = malloc(sizeof(Sprite));
sprite -> data  = malloc( width*length*sizeof(uint8_t)   );
memcopy( ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM