[英]C struct into array of struct pointers compile error
我有一個鏈表,我正在嘗試制作一個臨時數組,以幫助我解決每個節點,而我構建其余的結構,然后我打算釋放數組但似乎我無法存儲的地址將struct轉換為指針數組。
這是我遇到問題的簡化版本:
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:錯誤:從類型'struct vertex *'指定類型'vertex'時出現不兼容的類型
任何幫助將不勝感激。
編輯
這是結構
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;
vertexIndex
應該是指向指針的指針,因為您將它用作指針數組。
vertex **vertexIndex = NULL;
vertexIndex
不是struct數組。 它只是一個結構指針,這就是你得到錯誤的原因。
如果需要數組,請聲明一個數組:vertex
vertex *vertexHead[10]; //array of 10 pointers
現在,您將能夠像現在一樣使用它。
正如錯誤消息所示,在該行上,您正在嘗試將指針內容分配給指針,即。 將vertex *
指定的vertexHead
指定為*vertexIndex
(相當於vertexIndex[0]
,它們不兼容)。
發布vertex
定義代碼會更好,以便人們可以建議應該做什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.