首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不兼容的指针类型从链表中的'node *‘赋值给'int *’

不兼容的指针类型从链表中的'node *‘赋值给'int *’
EN

Stack Overflow用户
提问于 2020-04-27 01:56:08
回答 2查看 56关注 0票数 0
代码语言:javascript
运行
复制
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,我错了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-27 02:03:08

next字段的声明更改为正确的类型。您已经有了一个结构标记,因此可以在结构定义中引用它:

代码语言:javascript
运行
复制
struct str_node{
   int data;
   struct str_node *next;
}*head;
票数 1
EN

Stack Overflow用户

发布于 2020-04-27 01:59:34

只是编译器不能保证这些指针在所有机器上都是相同的大小。解决方案是(a)强制转换赋值= (int*)或(b)关闭警告。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61445483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档