结构编译错误 - 取消引用指向不完整类型的指针

时间:2021-09-20 04:51:56

I am trying to create a circular linked list, and im encounthering some errors. Looks like the errors is talking about derefencing pointer, when im trying to access the Employee struct from inside the Node.

我正在尝试创建一个循环链表,并且我正在加载一些错误。当我试图从Node内部访问Employee结构时,看起来错误正在讨论derefencing指针。

This is the errors I get:

这是我得到的错误:

 magic.c: In function ‘buildList’:
magic.c:49:2: error: dereferencing pointer to incomplete type
 n->e.id = 1;
  ^
magic.c:53:4: error: dereferencing pointer to incomplete type
   n->e = e;
    ^
magic.c:54:4: error: dereferencing pointer to incomplete type
   n->d = d; 
    ^
magic.c:55:8: error: dereferencing pointer to incomplete type
   n = n->next = (ptr)malloc(sizeof(node)); 
        ^
magic.c:58:3: error: dereferencing pointer to incomplete type
  n->next = head;
   ^
magic.c:58:12: error: ‘head’ undeclared (first use in this function)
  n->next = head;
            ^
magic.c:58:12: note: each undeclared identifier is reported only once for each function it appears in
magic.c: In function ‘printList’:
magic.c:68:17: error: dereferencing pointer to incomplete type
   printf("%d", n->e.id);
                 ^
magic.c:69:8: error: dereferencing pointer to incomplete type
   n = n->next;
        ^
make: *** [magic.o] Error 1

And this is my code:

这是我的代码:

   #include <stdio.h>
    #include <stdlib.h>

typedef unsigned int uint;

typedef struct {
    uint id;
    uint years;
    uint divion_id;
} employee;

typedef struct {
    uint id;
    char name[52];
    char manager_name[52];
    char telephone[16];
} division;

typedef struct node* ptr;

typedef struct {
    employee e;
    division d;

    ptr next;
} node;

typedef struct {
    ptr head;
} list;


list* buildList() 
{
    list *newList;
    uint i;
    employee e;
    division d;
    ptr n;

    e.id = 1;
    e.years = 5;
    e.divion_id = 1;

    newList = (list*)malloc(sizeof(list));
    newList->head = (ptr)malloc(sizeof(node));
    n = newList->head;

    for (i = 0; i < 5; ++i) {
        e.id = i+1;
        n->e = e;
        n->d = d;   
        n = n->next = (ptr)malloc(sizeof(node)); 
    }

    n->next = head;

    return newList;
}

void printList(list *lst) 
{
    ptr n = lst->head;

    do {
        printf("%d", n->e.id);
        n = n->next;
    } while (n != lst->head);
}




int main()
{
    list* lst;

    lst = buildList();
    printList(lst);
    return 0;
}

3 个解决方案

#1


5  

In the typedef of ptr you use the structure node, but you don't have such a structure, just a type-alias (a typedef).

在ptr的typedef中你使用结构节点,但你没有这样的结构,只有一个类型别名(typedef)。

Do e.g.

例如,做

typedef struct node {
    ...
} node;

#2


0  

First thing that I see it wrong is that you declare; typedef struct node* ptr; before declaring what is node, so you have to write typedef struct node* ptr; after node declaration..; not before.

我认为错误的第一件事是你宣布; typedef struct node * ptr;在声明什么是节点之前,所以你必须编写typedef struct node * ptr;节点声明后..;不是之前。

#3


0  

I think the problem here is that you have some kind of "incomplete type" deadlock scenario.
if we had deleted the ptr member inside the node structure like this:

我认为这里的问题是你有某种“不完整类型”的死锁场景。如果我们删除了节点结构中的ptr成员,如下所示:

typedef struct node* ptr;

typedef struct {
    employee e;
    division d;
} node;

then everything would have worked just fine. yes, it is true that ptr points to an incomplete type struct node but the struct node definition later, completes it.

那么一切都会好起来的。是的,ptr指向一个不完整的类型结构节点,但稍后结构节点定义完成它。

the problem is that we have this:

问题是我们有这个:

typedef struct node* ptr;

typedef struct {
    employee e;
    division d;

    ptr next;
} node;

ptr points to the incomplete type struct node but when we define struct node later we find that struct node itself is incomplete because it contains a member of incomplete type ptr so it can't complete struct node.

ptr指向不完整类型的struct节点,但是当我们稍后定义struct node时,我们发现struct node本身是不完整的,因为它包含一个不完整类型ptr的成员,因此它不能完成struct node。

#1


5  

In the typedef of ptr you use the structure node, but you don't have such a structure, just a type-alias (a typedef).

在ptr的typedef中你使用结构节点,但你没有这样的结构,只有一个类型别名(typedef)。

Do e.g.

例如,做

typedef struct node {
    ...
} node;

#2


0  

First thing that I see it wrong is that you declare; typedef struct node* ptr; before declaring what is node, so you have to write typedef struct node* ptr; after node declaration..; not before.

我认为错误的第一件事是你宣布; typedef struct node * ptr;在声明什么是节点之前,所以你必须编写typedef struct node * ptr;节点声明后..;不是之前。

#3


0  

I think the problem here is that you have some kind of "incomplete type" deadlock scenario.
if we had deleted the ptr member inside the node structure like this:

我认为这里的问题是你有某种“不完整类型”的死锁场景。如果我们删除了节点结构中的ptr成员,如下所示:

typedef struct node* ptr;

typedef struct {
    employee e;
    division d;
} node;

then everything would have worked just fine. yes, it is true that ptr points to an incomplete type struct node but the struct node definition later, completes it.

那么一切都会好起来的。是的,ptr指向一个不完整的类型结构节点,但稍后结构节点定义完成它。

the problem is that we have this:

问题是我们有这个:

typedef struct node* ptr;

typedef struct {
    employee e;
    division d;

    ptr next;
} node;

ptr points to the incomplete type struct node but when we define struct node later we find that struct node itself is incomplete because it contains a member of incomplete type ptr so it can't complete struct node.

ptr指向不完整类型的struct节点,但是当我们稍后定义struct node时,我们发现struct node本身是不完整的,因为它包含一个不完整类型ptr的成员,因此它不能完成struct node。