【问题标题】:C : Dereferencing pointer to incomplete type errorC:取消引用指向不完整类型错误的指针
【发布时间】:2017-11-02 18:41:25
【问题描述】:

我在 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;
}

我想知道为什么我会收到这个错误(尽管定义了 struct contact.)。谢谢。

【问题讨论】:

  • 可能是因为你定义了struct contact,但是temp被定义为指向struct student的指针。
  • 您好,欢迎来到 StackOverflow!遗憾的是,您没有提供足够的代码让我们能够正确地帮助您解决问题。请阅读How to Ask 并使用minimal reproducible example 更新您的问题。

标签: c struct dereference


【解决方案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;
}

【讨论】:

  • 抱歉那部分没问题,我在显示错误行时出错了。我现在更正了。
  • 你已经定义了struct student *temp,你可以发布你的student声明吗?
  • "Struct student" 本身就是一个类型,我在顶部定义了它。
  • 当您说struct student *temp = *hptr; 时,您基本上是在实例化student 类型的指针。你需要在某处声明`struct student`
  • 这不是 decleration(我在代码顶部制作的)吗?:struct student { char name[100];字符数[20];结构学生*下一个; };
猜你喜欢
  • 2018-05-31
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 2016-07-12
  • 2018-04-09
相关资源
最近更新 更多