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

``````#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();

POBJECT bouncer = &ball;
POBJECT targetArray[ARRAYSIZE];
targetArray[0] = &block;

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);
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;
``````

``````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;
}
``````

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

``````POBJECT bouncer = &ball;
``````

0 条评论