为了账号安全,请及时绑定邮箱和手机立即绑定

为什么会出现程序崩溃?!!

为什么会出现程序崩溃?!!

C PHP
暮色呼如 2023-03-16 17:13:21
#include <stdio.h>#include <stdlib.h>#include <time.h>typedef struct Node{int data;struct Node *next;}Node, *pt_Node;//初始化生成头结点static int InitList(pt_Node L)//为什么是*L才正确???就这里也不懂{L = (pt_Node)malloc(sizeof(Node));if (!L){return -1;}L->next = NULL;return 0;}/*统计链表的长度*/static int ListLenth(pt_Node L){int i = 0;pt_Node p = L->next;while (p){i++;p = p->next;}return i;}int main(void){int lenth;pt_Node link;//不能是*link?为什么InitList(link);lenth = ListLenth(link);printf("the lenth is %d", lenth);return 0;}
查看完整描述

1 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

这个代码的流程 ,是在主函数中定义一个头节点link
然后通过函数InitList来给头节点赋值。 
既然要给link赋值,就需要参数是link的指针类型,否则无法传回主函数。
于是需要定义成
static int InitList(pt_Node *L)
同时在函数中给*L赋值。 
调用的时候 用InitList(&link);

查看完整回答
反对 回复 2023-03-18
  • 1 回答
  • 0 关注
  • 50 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信