简体   繁体   English

C:取消引用指向不完整类型错误的指针

[英]C : Dereferencing pointer to incomplete type error

I created a link list of contacts in C. It worked fine. 我在C中创建了一个联系人链接列表。它运行正常。 but now I want to write a delete function for a specified contact (by name) I get the Error:"dereferencing pointer to incomplete type" . 但现在我想为指定的联系人写一个删除函数(按名称)我得到Error:"dereferencing pointer to incomplete type" Here is my code: 这是我的代码:

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

I wanted to know why i get this error (despite defining struct contact.). 我想知道为什么我得到这个错误(尽管定义了struct contact。)。 Thank you. 谢谢。

Your next pointer type was contact - assuming that was a typo - here's the corrected code with the typos fixed - This compiles- HTH! 你的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;
}

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

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