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

滑翔机

我的问题是我不知道如何正确初始化包含要在小矩阵显示器上绘制的块的数组。初始化第一个对象有时是可行的(阻止),但是通过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;

而不是用板坯

返回一个int,不是void

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章