【发布时间】:2021-01-18 20:30:27
【问题描述】:
我不知道为什么会出现这个警告 我试图写一个代码来删除链表中的重复数据 我意识到的一点是,当我评论包含免费命令的行时,警告不会发生! 我在 Visual Studio 2019 编译器中收到了这个警告 请告诉我它的用途以及如何解决此问题
#include <stdio.h>
#include <stdlib.h>
typedef struct list List;
struct list
{
int data;
List* next;
};
List* remove_repetition(List* list)
{
List* pre = list;
if (pre == NULL)
return NULL;
List* now = list->next;
while (now != NULL)
{
if (pre->data == now->data)//the warning occurs here!
{
pre->next = now->next;
List* del = now;
now = now->next;
free(del);
}
else
{
pre = now;
now = now->next;
}
}
return list;
}
void out(List* head)
{
List* now = head;
while (now != NULL)
{
printf("%d\t", now->data);
now = now->next;
}
return;
}
int main() {
int i;
List* head1 = (List*)malloc(sizeof(List));
if (head1 == NULL)
exit(1);
List* head_temp = head1;
int data[8] = { 1,1,1,1,5,5,5,10 };
for (i = 0; i < 7; i++) {
head_temp->data = data[i];
List* next = (List*)malloc(sizeof(List));
if (next == NULL)
exit(1);
head_temp->next = next;
head_temp = next;
}
head_temp->data = data[i];
head_temp->next = NULL;
out(remove_repetition(head1));
}
【问题讨论】:
-
代码看起来很好,
gcc -Wall -Wextra -pedantic没有给出任何警告。看起来像是一个错误的警告。 -
这里:stackoverflow.com/questions/59238295/… 建议尝试使用
calloc而不是malloc来消除错误警告。不确定它是否适合您... -
@WernerHenze 这个问题用 C 标记,而不是 C++。
-
众所周知,Microsoft 的编译器......嗯......不太适合 C。使用符合标准的编译器。
标签: c memory initialization warnings