一个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;
}