简体   繁体   English

C struct into struct pointers数组编译错误

[英]C struct into array of struct pointers compile error

I've got a linked list and I'm trying to make a temporary array to help me address each node while I build the rest of the structure, then I intend to free the array but it seems I can't store the address of the struct into a pointer array. 我有一个链表,我正在尝试制作一个临时数组,以帮助我解决每个节点,而我构建其余的结构,然后我打算释放数组但似乎我无法存储的地址将struct转换为指针数组。

Here's a boiled down version of where I'm having problems: 这是我遇到问题的简化版本:

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 *' main.c:72:19:错误:从类型'struct vertex *'指定类型'vertex'时出现不兼容的类型

Any help would be greatly appreciated. 任何帮助将不胜感激。

EDIT 编辑

Here are the structs 这是结构

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 is not an array of struct. vertexIndex不是struct数组。 It's just a struct pointer which is why you are getting the error. 它只是一个结构指针,这就是你得到错误的原因。

If you want an array, declare an array: vertex 如果需要数组,请声明一个数组:vertex

vertex *vertexHead[10]; //array of 10 pointers

Now, you'll be able to use it as you do now. 现在,您将能够像现在一样使用它。

As the error message says, on that line you are trying to assign content of pointer to the pointer ie. 正如错误消息所示,在该行上,您正在尝试将指针内容分配给指针,即。 assigning vertexHead which is vertex * to *vertexIndex (equivalent to vertexIndex[0] which are not compatible. vertex *指定的vertexHead指定为*vertexIndex (相当于vertexIndex[0] ,它们不兼容)。

It will be better you post your code of definition of vertex so that people can suggest what should be done preferably. 发布vertex定义代码会更好,以便人们可以建议应该做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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