我是新來的C,我不能弄清楚,爲什麼我收到初始化不相容poniter型warning.The代碼的相關部分:編譯器警告:初始化兼容的指針類型
struct a {
int address;
B * apples[8];
} A;
struct b {
int color;
} B;
我有一個名爲a_list的A數組。什麼我目前做的是:
B *b_list = a_list[i].apples; // i being an index in for loop
b_list[6].color = 0;
上述工作正常,但拋出的警告:
Initialization from incompatible pointer type.
爲了解決這個問題,我的理由是我應該做的
B ** b_list = a_list[i].apples; // as it is pointer to pointer.
So now b_list basically points to a pointer which points to an array of 6, yea?
So: (*b_list)[6].color // However this causes segmentation fault.
當結構體A有B蘋果[8]而不是B *蘋果[8]時,它也正常工作。但是,使用這種解決方案並不能將功能所做的更改保留在制定位置之外。
請指教。
上述兩者都會導致分段錯誤。是否因爲有些街區沒有搭配? – Catie
@Catie:你有沒有分配數組中的每個'B'? –