C では、「初期化子要素が定数ではありません」というエラーを解決する 1 つの方法は、main() 関数内に strcuture を作成することです。しかし、構造体の配列があり、それをグローバル配列として使用したいとします。どうすればそれを作成して初期化できますか?
struct A *b = malloc(10*sizeof(struct A)); // Want to keep the malloc
void init_A_types(struct A* t)
{
t->elm1=0; t->elm2=1;
}
...
int Main() {
for (k=0;k<10;k++)
init_A_types(b+k);
...
return 0;
}