在结构中初始化指向2D数组的指针

时间:2021-01-14 21:40:45

What I try to get, is a structure, which contains an 2D array of bytes which should store a binary picture.

我试图得到的是一个结构,它包含一个2D字节数组,应该存储二进制图片。

typedef enum{
    PIC_ID_DOUBLE_UP,
    PIC_ID_DOUBLE_DOWN,
}PICPictureId_t;

typedef struct{
    PICPictureId_t  picId;
    uint8           width;
    uint8           height;
    uint8           (**dataPointer);
}PICPicture_t;

The dimensions (in pixels) are determined by the width and height. Now I tried to initialize an array of pictures. I want to store the whole data in the flash.

尺寸(以像素为单位)由宽度和高度决定。现在我尝试初始化一系列图片。我想将整个数据存储在闪存中。

PICPicture_t pictures[] = {
    {
        .height       = 3,
        .width        = 16,
        .picId        = PIC_ID_DOUBLE_UP,
        .dataPointer  = ???        
    }
};

How could I initialize the pointer to the data? I get a version which compiles (after studying the answer here: A pointer to 2d array), if I use a pointer to an array in the picture struct and then set the pointer to the first element of the doubleUp 2D array:

我怎么能初始化指向数据的指针?我得到一个编译的版本(在研究答案之后:指向2d数组的指针),如果我使用指向图片结构中的数组的指针,然后将指针设置为doubleUp 2D数组的第一个元素:

typedef struct{
    PICPictureId_t  picId;
    uint8           width;
    uint8           height;
    uint8           (*dataPointer)[2];
}PICPicture_t;

uint8 doubleUp[3][2] = {
    {0x12 ,0x13},
    {0x22 ,0x32},
    {0x22 ,0x32}
};


PICPicture_t pictures[] = {
    {
        .height       = 3,
        .width        = 16,
        .picId        = PIC_ID_DOUBLE_UP,
        .dataPointer  = &(doubleUp[0]),
    }
};

But here I have to declare the dimension of the second array but I want to make the structure idependent of the dimension and use for this the height and width field.

但在这里我必须声明第二个数组的维度,但我想使结构与维度相关,并使用高度和宽度字段。

1 个解决方案

#1


2  

Use a pointer to a one-dimensional array and index it manually:

使用指向一维数组的指针并手动索引:

typedef struct{
PICPictureId_t  picId;
uint8           width;
uint8           height;
uint8           *dataPointer;
}PICPicture_t;

The image data will have to change to a single dimension:

图像数据必须更改为单个维度:

uint8 d[6] = {
    0x12 ,0x13,
    0x22 ,0x32,
    0x22 ,0x32
};

You can initialize it:

你可以初始化它:

PICPicture_t s = { 3 , 2, ID , d }; 

And interpret it as a 2d array:

并将其解释为2d数组:

uint8 x = 1;
uint8 y = 2;
uint8 value = s.dataPointer[y*width+x];  

(I changed the width to 2 from 16 so the example is clearer. The idea is the same if you plan to access single bits. )

(我将宽度从16更改为2,因此示例更清晰。如果您计划访问单个位,则想法是相同的。)

#1


2  

Use a pointer to a one-dimensional array and index it manually:

使用指向一维数组的指针并手动索引:

typedef struct{
PICPictureId_t  picId;
uint8           width;
uint8           height;
uint8           *dataPointer;
}PICPicture_t;

The image data will have to change to a single dimension:

图像数据必须更改为单个维度:

uint8 d[6] = {
    0x12 ,0x13,
    0x22 ,0x32,
    0x22 ,0x32
};

You can initialize it:

你可以初始化它:

PICPicture_t s = { 3 , 2, ID , d }; 

And interpret it as a 2d array:

并将其解释为2d数组:

uint8 x = 1;
uint8 y = 2;
uint8 value = s.dataPointer[y*width+x];  

(I changed the width to 2 from 16 so the example is clearer. The idea is the same if you plan to access single bits. )

(我将宽度从16更改为2,因此示例更清晰。如果您计划访问单个位,则想法是相同的。)