InitList(struct List L)函数在调用的时候,采用的是值传递的方式,所以,在main函数中的调用没有对结构体变量a初始化:
将该函数的参数改为结构体变量的指针,然后在主函数中传递结构体变量a的地址就不会报错了,就可以了
InitList(struct List *L)
{
(*L).elem=(int *)malloc(10*sizeof(int));
if(!(*L).elem) printf("error!!!\n");
(*L).length=0;
(*L).listsize=0;
printf("OK!!!!\n");
}
...........
............
.............
InitList(&a);
............
...........
...........
#include
#include
struct List
{
int *elem;
int length;
int listsize;
};
void InitList(struct List L)
{
L.elem=(int *)malloc(10*sizeof(int));
if(!L.elem) printf("error!!!\n");
L.length=0;
L.listsize=0;
printf("OK!!!!\n");
}
int main()
{
struct List a;
int *head;
InitList(a);
head=a.elem;
while(a.elem)
{
scanf("%d\n",a.elem);
a.elem++;
a.length++;
}
a.elem=head;
while(a.elem)
{ printf("%5d",a.elem);
a.elem++;
}
printf("\n%5d\n",a.length);
}