簡體   English   中英

嘗試創建鏈表但指針分配錯誤

[英]Trying to create a linked list but error with pointer assignment

我正在嘗試制作一個鏈表並創建一些方法。 但是,我得到了錯誤:

賦值使指針從整數開始而無需強制轉換。

這是我的代碼:

#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;
}

如果錯誤在注釋行上,則ID可能是指針,而不是int。 這將正常工作:

students->ID = 0; 

因為它將指針設置為NULL,所以編譯時不會出現錯誤/警告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM