我在 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/