简体   繁体   中英

C : Dereferencing pointer to incomplete type error

I created a link list of contacts in C. It worked fine. but now I want to write a delete function for a specified contact (by name) I get the 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.). 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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