我有以下代碼:分配到陣列中的結構在C
typedef struct Test {
long mem[1000];
} Test;
extern Test *test;
int main() {
Test *test = (Test *)malloc(sizeof(Test));
test->mem[0] = 1;
test->mem[1] = 2;
test->mem[2] = 3;
test->mem[3] = 4;
test->mem[4] = 5;
test->mem[5] = 6;
return 0;
}
它的工作好,但我想改變MEM陣列中的初始化是在這樣:
test->mem = {1,2,3,4,5,6};
但海灣合作委員會給我這個錯誤:
error: expected expression before '{' token test->mem = {1,2,3,4,5,6}; With arrow pointing to the left open braces.
它可能是什麼?
謝謝!
編輯: 我也試試這個代碼:
long mem[1000] = {1,2,3,4,5,6};
test->mem = mem;
而且我從GCC收到此錯誤:
error: incompatible types when assigning to type 'long int[1048576]' from type 'long int *' test->mem = mem;
我不允許使用任何C函數。
請參閱問題中的編輯。 – Nir