开发者

Trying to create a linked list but error with pointer assignment

开发者 https://www.devze.com 2022-12-27 11:16 出处:网络
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.

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->开发者_JAVA技巧;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.

0

精彩评论

暂无评论...
验证码 换一张
取 消