struct str_node{
int data;
int *next;
}*head;
typedef struct str_node node;
void create_list(int n){
node *boh,*tmp;
int num,i;
head = (node*)malloc(sizeof(node));
if(head == NULL){
printf("Memory can not be allocated.");
}
else{
printf("Insert value for node 1: ");
scanf("%d",&num);
head->data = num;
head->next = NULL;
tmp = head;
for(i=2;i<=n;i++){
boh = (node*)malloc(sizeof(node));
if(boh ==NULL){
printf("Memory can not be allocated.");
break;
}
else{
printf("Insert value for node %d",i);
scanf("%d",&num);
boh->data = num;
boh->next = NULL;
tmp->next = boh; //<--Incompatible pointer types assigning to 'int *' from 'node *' (aka 'struct str_node *')
tmp = tmp->next; //<-- Incompatible pointer types assigning to 'node *' (aka 'struct str_node *') from 'int *'
}
}
}
}
代码运行得很好,但我不理解这两个错误。你能给我解释一下吗?我不认为有任何int,我只使用struct node,我错了吗?
发布于 2020-04-27 02:03:08
将next
字段的声明更改为正确的类型。您已经有了一个结构标记,因此可以在结构定义中引用它:
struct str_node{
int data;
struct str_node *next;
}*head;
发布于 2020-04-27 01:59:34
只是编译器不能保证这些指针在所有机器上都是相同的大小。解决方案是(a)强制转换赋值= (int*)
或(b)关闭警告。
https://stackoverflow.com/questions/61445483
复制相似问题