繁体   English   中英

C struct into struct pointers数组编译错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM