【问题标题】:Malloc function shows errorMalloc 函数显示错误
【发布时间】:2015-12-19 13:18:27
【问题描述】:
`struct node * createLL(struct node *head)
{   
int num;
struct node *new_node;
printf("enter the numbers you want to insert:\n");
printf("enter -1 to quit");
scanf("%d",&num);
while(num!=-1)
{
new_node=(struct node *) malloc (sizeof(struct node *));
new_node->data=num;
if(head==NULL)
{
new_node->next=NULL;
head=new_node;
}
else
{
new_node->next=head;
head=new_node;
}
printf("enter the numbers you want to insert:\n");
printf("enter -1 to quit");
scanf("%d",&num);
}
return head;
}`

用于为 struct node* 类型的链表创建 new_node 的 malloc 显示错误。它说“malloc 未在范围内声明”.. 我也参考了书籍..代码相同..无法弄清楚错误是如何发生的 可以改正的。

【问题讨论】:

  • malloc 应该在 stdlib.h 中声明 - 你确定你做了#include <stdlib.h>

标签: malloc


【解决方案1】:

您确定在标题中包含正确的库吗?确保包含

#include  <stdlib.h>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    相关资源
    最近更新 更多