对顺序表的赋值操作(C语言)查错!(高分急!!!)

来源:百度知道 编辑:UC知道 时间:2024/06/18 09:19:21
小妹是个新手,写了个很短的顺序表操作的代码,看来看去就是不知道哪里错了!编译总是不让通过,我真的很疑惑,有没有高人指点一下!!!
PS:我写的代码不好,但我在后面都有注解,只求有人指点!!
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3
typedef struct {
int num;
char name[20];
int grade;
int score;
}Student;//定义学生的信息
typedef struct {
Student data[SIZE];
int length;
int listsize;
}Sqlist;//顺序表存储
main(){
Sqlist *L;
L->data=(Student*)malloc(SIZE*sizeof(Student));
int i;
printf("请输入学生相关数据:\n");
for(i=0;i<SIZE;i++)
scanf("%d%s%d%d",&L->data[i].num,L->data[i].name,&L->data[i].grade,&L->data[i].score);
for(i=0;i<SIZE;i++)
printf("%d %s %d %d\n",L->data[i].num,L->data[i].name,L->data[i].grade,L->data[i].score);
}

如果我不加malloc就可以通过并实现功能,但是L就不可以是指针变量。如果非要定义成指针变量该怎么做呢?
我也看了很多别人写的代码,一般教材里ElemType都定义成int或者别的简单的数据类型,如果ElemType也是结构体我弄了好半天都不能成功,期待高人指点!!!!!

Sqlist* L;
L->data=(Student*)malloc(SIZE*sizeof(Student));
指针是L,你因该是给L申请空间,怎么变成给L->data了
改成L=(Sqlist*)malloc(sizeof(Sqlist));就行了
不必担心L->data没有分配空间,Sqlist里面已经包含了
给L分配空间后data数组里面的每一个元素只有student的4个信息,数组里面并不包含Length和listsize,换句话说Length和listsize只有一个

/*如果你非要用指针的话,应该这么改*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3
typedef struct {
int num;
char name[20];
int grade;
int score;
}Student;//定义学生的信息

typedef struct {
Student* data;
int length;
int listsize;
}Sqlist;//顺序表存储

main()
{
Sqlist* L;
int i;
L=(Sqlist*)malloc(sizeof(Sqlist));
L->data=(Student*)malloc(SIZE*sizeof(Student));
printf("请输入学生相关数据:\n");
for(i=0;i<SIZE;i++)
scanf("%d%s%d%d",&L->data[i].num,L->data[i].name,&L->data[i].grade,&L->data[i].score);
for(i=0;i<SIZE;i++)
pr