c/c++中使用结构体变量报错使用了未初始化变量L或者vs code运行可执行文件没有像预想的输出到终端
一个c/c++小问题起先我的代码是这样的:#include"list.h"#include<stdio.h>int main(){List *L;ElemType x=10;InitList(L,x);printf("%d\n",L->first->next->data);printf("%d\n",L->last->next->data);retu
·
一个c/c++小问题
起先我的代码是这样的:
#include"list.h"
#include<stdio.h>
int main()
{
List *L;
ElemType x=10;
InitList(L,x);
printf("%d\n",L->first->next->data);
printf("%d\n",L->last->next->data);
return 0;
}
然后就报错如标题,后来发现是没有给list这个结构体申请内存空间的缘故,因此修改代码如下:
#include"list.h"
#include<stdio.h>
int main()
{
List *L= (List*)malloc(sizeof(List));
//或者使用这个语句
//List L=new List;
ElemType x=10;
InitList(L,x);
printf("%d\n",L->first->next->data);
printf("%d\n",L->last->next->data);
return 0;
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)