试图创造一个链表但指针赋值错误 [英] Trying to create a linked list but error with pointer assignment

查看:195
本文介绍了试图创造一个链表但指针赋值错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个链表,并创建一些方法。不过,我得到的错误:


  

分配时将整数指针不进行强制转换。


下面是我的code:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括students.h
node_ptr创建(无效)
{
    node_ptr学生=(node_ptr)的malloc(sizeof的(结构节点));
    学生 - 和SEQ ID = 0;
    学生 - >名= NULL;
    学生 - >接着= NULL;
    返回的学生;
}无效insert_in_order(INT N,node_ptr名单)
{
    node_ptr之前=清单;
    node_ptr new_node =(node_ptr)的malloc(sizeof的(结构节点));
    new_node-> ID = N; //错误是在这里,我想
    而(直至─>接下来&放大器;及(直至─>下一步 - > ID< N))
    {
        之前=直至─>接下来,
    }    new_node->接下来=直至─>接下来,
    直至─>接下来= new_node;
}


解决方案

如果错误是在注释行,那么也许ID是一个指针,而不是一个int。这将正常工作:

 学生 - > ID = 0;

因为它设置指针为NULL,所以它编译没有错误/警告。

I am trying to make a linked list and create some methods. However, I am getting the error:

Assignment makes pointer from integer without a cast.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include "students.h"


node_ptr create(void)
{
    node_ptr students = (node_ptr) malloc(sizeof(struct node));
    students->ID = 0; 
    students->name = NULL;
    students->next = NULL; 
    return students;
}

void insert_in_order(int n, node_ptr list)
{
    node_ptr before = list;
    node_ptr new_node = (node_ptr) malloc(sizeof(struct node));
    new_node->ID = n; //error is here I think


    while(before->next && (before->next->ID < n))
    {
        before = before->next;
    }

    new_node->next = before->next;
    before->next = new_node;
}

解决方案

If the error is on the commented line, then maybe ID is a pointer, not an int. This will work fine:

students->ID = 0; 

because it sets the pointer to NULL, so it compiles without error/warning.

这篇关于试图创造一个链表但指针赋值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆