繁体   English   中英

如何正确初始化 C 结构数组

[英]How to correctly initialize C struct array

我的问题是我不知道如何正确初始化包含要在小型矩阵显示器上绘制的块的数组。 初始化第一个 object 有时会起作用(块),但通过 for 循环初始化剩余对象似乎没有任何作用。 我会很高兴有任何帮助!

结构:

#define MAX_POINTS 50
#define ARRAYSIZE 2

typedef enum {false, true} bool;

typedef struct tpoint {
uint8_t x;
uint8_t y;
} POINT;

typedef struct tGeometery {
int numpoints;
int sizex;
int sizey;
POINT px[ MAX_POINTS ];
} GEOMETRY, *PGEOMETRY;

typedef struct tObj {
    PGEOMETRY geo;
    int dirx,diry;
    int posx,posy;
    void (* draw_object ) (struct tObj *, bool draw);
    void (* move_object ) (struct tObj *);
    void (* set_object_speed ) (struct tObj *, int, int);
} OBJECT, *POBJECT; 

“对象”:

GEOMETRY ball_geometry = {
    12,     //Numpoints
    4,4,    //size X,Y
    {
              {1,0},{2,0},
        {0,1},{1,1},{2,1},{3,1},
        {0,2},{1,2},{2,2},{3,2},
              {1,3},{2,3}
    }
};

OBJECT ball = {
    &ball_geometry,
    0,0,    //Direction
    10,1,   //Start position
    draw_object,
    move_object,
    set_object_speed
};

GEOMETRY slab_geometry = {
    30,     //Numpoints
    10,3,   //Size X,Y
    {
        {0,0},{1,0},{2,0},{3,0},{4,0},{5,0},{6,0},{7,0},{8,0},{9,0},
        {0,1},{1,1},{2,1},{3,1},{4,1},{5,1},{6,1},{7,1},{8,1},{9,1},
        {0,2},{1,2},{2,2},{3,2},{4,2},{5,2},{6,2},{7,2},{8,2},{9,2}
    }
};

OBJECT slab = {
    &slab_geometry,
    0,0,    //Direction
    59,61,  //Start position (128/2 - 5)
    draw_object,
    move_object,
    set_object_speed
};

GEOMETRY block_geometry = {
    4,      //Numpoints
    2,2,    //Size X,Y
    {
        {0,0},{1,0},
        {1,0},{1,1}
    }
};

OBJECT block = {
    &block_geometry,
    0,0,    //Direction
    0,0,    //Start position
    draw_object,
    move_object,
    set_object_speed
};

主要的:

void main(void) {
graphic_init();
keypad_init();

POBJECT bouncer = &ball;
POBJECT paddle = &slab;
POBJECT targetArray[ARRAYSIZE];
targetArray[0] = █

for (int i = 1; i < ARRAYSIZE; i++) {
    targetArray[i] = &block;
    targetArray[i]->posx = targetArray[i-1]->posx+3;
}

bouncer->set_object_speed(bouncer,4,1);
paddle->draw_object(paddle,true);
for (int i = 0; i < ARRAYSIZE; i++) {
    targetArray[i]->draw_object(targetArray[i],true);
}

while(1) {
    bouncer->move_object(bouncer);
}

}

正在做

POBJECT targetArray[ARRAYSIZE]; targetArray[0] = &block; for (int i = 1; i < ARRAYSIZE; i++) { targetArray[i] = &block; targetArray[i]->posx = targetArray[i-1]->posx+3; }

targetArray的所有元素都是相同的(例如block ),这不是您想要的,因为要做

targetArray[i]->posx = targetArray[i-1]->posx+3;

实际上与以下内容相同:

 targetArray[0]->posx = targetArray[0]->posx+3;

最终做到了:

 block.posx = block.posx+3;

for (int i = 0; i < ARRAYSIZE; i++) { targetArray[i]->draw_object(targetArray[i],true); }

与以下内容相同:

 for (int i = 0; i < ARRAYSIZE; i++) {
   targetArray[0]->draw_object(targetArray[0],true);
 }

最终做到了:

 for (int i = 0; i < ARRAYSIZE; i++) {
   block.draw_object(&block,true);
 }

你为什么这么做

POBJECT targetArray[ARRAYSIZE];

而不是

OBJECT targetArray[ARRAYSIZE];

并在tObj

 PGEOMETRY geo;

而不是

GEOMETRY geo;

?

使用POBJECTPGEOMETRY您需要为targetArray的每个条目和每个字段geo一个新元素,例如

targetArray[0] = malloc(sizeof(*(targetArray[0])));
*(targetArray[0]) = block;
targetArray[0]->geo = malloc(sizeof(*(targetArray[0]->geo)));
*(targetArray[0]->geo) = *(block.geo);
for (int i = 1; i < ARRAYSIZE; i++) {
  targetArray[i] = malloc(sizeof(*(targetArray[i])));
  *(targetArray[i]) = block;
  targetArray[i]->geo = malloc(sizeof(*(targetArray[i]->geo)));
  *(targetArray[i]->geo) = *(block.geo);
  targetArray[i]->posx = targetArray[i-1]->posx+3;
}

但是使用OBJECTGEOMETRY

targetArray[0] = block;
for (int i = 1; i < ARRAYSIZE; i++) {
  targetArray[i] = block;
  targetArray[i].posx = targetArray[i-1].posx+3;
}

通过typedef隐藏指针是一个坏主意,我鼓励您永远不要这样做以使*可见。

我也不明白你为什么这样做

POBJECT bouncer = &ball; POBJECT paddle = &slab;

而不是在main中使用ballslab

主要返回一个int ,而不是void

暂无
暂无

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

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