我在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
-假设是错字 - 这是固定的键入的校正代码 - 此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;
}