我对C相当陌生,想知道我是否可以先初始化一个设定大小的结构数组,然后在声明后用实际的结构填充数组。下面的代码片段展示了我想要做的事情,它应该非常简单。
/* Make rectangle 2D object */
struct two_d_obj rect = {0, 4, {0, 0, 1}, {0, 0}, {0, -20.0}, {{0, 0}, {0.1, 0.1}, {0, 0.1}, {0.1, 0}}};
struct two_d_obj obj_array[25];
obj_array[0] = rect;
但是,在尝试编译此代码时,出现以下错误:
hellomousept2.c:39: error: conflicting types for ‘obj_array’
hellomousept2.c:33: error: previous definition of ‘obj_array’ was here
hellomousept2.c:39: error: invalid initializer
同样,我是 C 的新手,主要是 Java 的代码,所以任何帮助我走上正轨的帮助都将受到高度赞赏,并提前致谢。
编辑:下面是我的 two_d_obj 结构的代码
struct two_d_obj
{
int iType; /*integer signifying shape of object (0 for rect, 1 for circle) */
int num_vertices; /* number of vertices contained in the shape */
double color[3]; /*array containing RGB values signifying color of object */
double center_pos[2]; /*center position of object */
double velocity[2]; /*velocity of object */
double vertex_array[50][2]; /*array of vertice coordinates (read in pairs
x coordinate followed by y coordinate)
protocol: first pair of coordinates is bottom left
vertice, pairs that follow are vertices going
counter-clockwise
*/
};