C : Dereferencing pointer to incomplete type error

标签 c struct dereference

我在 C 中创建了一个联系人链接列表。它运行良好。但现在我想为指定的联系人(按名称)编写一个删除函数,我得到了 Error:"dereferencing pointer to incomplete type"。这是我的代码:

struct contact
{
    char name[100];
    char number[20];
    struct contact *next;
};
int deleteByName(struct contact **hptr, char *name)
{
    struct student *prev = NULL;
    struct student *temp = *hptr;
    while (strcmp(temp->name /*The Error is Here*/ , name) != 0 && (temp->next) != NULL)
    {
        prev = temp;
        temp = temp->next; 
    }
    if (strcmp(temp->name, name) == 0)
    {
        if (prev == NULL)
            *hptr = temp->next;
        else
            prev->next = temp->next;
        free(temp);
        return 0;
    }
    printf("\nNAME '%s' WAS NOT FOUND TO BE DELETED.", name);
    return -1;
}

我想知道为什么会出现此错误(尽管定义了结构联系。)。谢谢。

最佳答案

您的 next 指针类型是 contact - 假设这是一个拼写错误 - 这是更正的代码,拼写错误已修复 - 这编译 - HTH!

struct student
{
    char name[100];
    char number[20];
    struct student *next;
};

int deleteByName(struct student **hptr, char *name)
{
    struct student *prev = NULL;
    struct student *temp = *hptr;
    while (strcmp(temp->name, name) != 0 && (temp->next) != NULL)
    {
        prev = temp;
        temp = temp->next; //***No Error now***
    }
    if (strcmp(temp->name, name) == 0)
    {
        if (prev == NULL)
            *hptr = temp->next;
        else
            prev->next = temp->next;
        free(temp);
        return 0;
    }
    printf("\nNAME '%s' WAS NOT FOUND TO BE DELETED.", name);
    return -1;
}

关于C : Dereferencing pointer to incomplete type error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44310525/

相关文章:

C 语法 : Is the following 'extern volatile const' behavior consistent among C compilers?

c - 标记字符串并将其作为数组返回

c - 如何识别 linux 系统调用 unshare(CLONE_NEWNET) 中的性能瓶颈

c - 尝试从文本文件中读取数据,从中创建结构,并将字段打印到标准输出

c++ - 如何序列化结构并将其存储在 MySQL 数据库中?

c - 在结构错误中指向取消引用

union 访问成本与使用基本类型

c++ - gcc 和 g++ 错误 : error trying to exec 'cc1plus' : execvp: No such file or directory

C pthread 和 struct 问题

c++ - 使用指向类的指针调用成员函数时获取 System.AccessViolationException