我有一個鏈表,我試圖做一個臨時數組來幫助我解決每個節點,而我建立其餘的結構,然後我打算釋放數組,但似乎我不能將結構的地址存儲到指針數組中。C結構到結構指針數組編譯錯誤
這裏是我有問題,其中的歸結版本:
vertex *vertexIndex = NULL;
vertex *vertexHead = NULL;
vertexHead = malloc(sizeof(vertex));
vertexHead->vertexNum = 5;
vertexIndex = malloc(sizeof(vertex*));
vertexIndex[0] = vertexHead; //<<<<<<<<<<< Error on this line
printf("%u\n", (vertexHead[0])->vertexNum);
main.c:72:19: error: incompatible types when assigning to type ‘vertex’ from type ‘struct vertex *’
任何幫助將不勝感激。
EDIT
下面是結構
struct edgeStruct {
unsigned int edgeTo;
struct edgeStruct *nextEdge;
};
typedef struct edgeStruct edge;
struct vertexStruct {
unsigned int vertexNum;
edge *edgeHead;
struct vertexStruct *nextVertex;
};
typedef struct vertexStruct vertex;
我們可以看到vertexHead和vertexIndex的聲明? –
懷疑問題是與'sizeof(vertex *)'?雖然你似乎通過生成一個指針來填充數組? ?! – Andrew