私は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
*/
};