我有这个代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct{
int data;
struct node *next;
} NODE;
NODE *head = NULL;
void display_ll(NODE *head){
if (head == NULL){
printf("\nList is empty");
} else{
for(NODE *curr=head; curr!=NULL; curr=curr->next){
printf("%d ->", curr->data);
}
printf("\b\b ");
}
}
int main(void){
NODE *temp = (NODE *)malloc(sizeof(NODE));
temp->data = 5;
temp->next = NULL;
head = temp;
display_ll(head);
return 0;
}
在第 15 行,即我在循环curr=curr->next
中执行的操作for
,如果使用 clang 进行编译,则会收到警告,如果使用 gcc 进行编译,则会收到错误:
- 铛:
warning: incompatible pointer types assigning to 'NODE *' from 'struct node *' [-Wincompatible-pointer-types]
- 海湾合作委员会:
error: assignment to ‘NODE *’ from incompatible pointer type ‘struct node *’ [-Wincompatible-pointer-types]`
我想到了一种解决方法,我们将结构写为,typedef struct node {...} NODE;
这样就可以了。但我想知道是否有任何方法可以解决这个问题,而无需重新定义这个结构。