开发者社区> 问答> 正文

C语言中NULL 的问题 400 请求报错

在写一段小程序的时候,遇到了问题。
问题1:为什么在Init 函数中L->next 等于NULL,而在main函数中不等?NULL 在C中是怎么定义的?
问题2:为什么释放L的时候会出错呢?是和L定义的位置有关吗?
代码如下: 

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

struct node{ int data; struct node *next; };

void Init(struct node *L){ struct node *tempNode; tempNode = (struct node *) malloc(sizeof(struct node)); tempNode->next = NULL; /*next 为 NULL */ L = tempNode; if(L->next == NULL) /判断下一个是否为NULL/ printf("Init ... L->next == NULL\n"); /结果为 等于NULL/

else
	printf("Init ... L->next != NULL\n");	

}

void Destory(struct node *L){ free(L); }

int main(){ struct node *L; Init(L); if(L->next == NULL) /*判断下一个是否为NULL */ printf("main ... L->next == NULL\n"); else printf("main ... L->next != NULL\n");/结果为不等于NULL/ Destory(L); /释放时出错/ return 0; }

谢谢回答!

展开
收起
kun坤 2020-05-29 15:51:55 669 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多