刚学数据结构不久,大神们为啥输出的是这个呀
一种改法是:
把main函数中的SqMenuList *L;改成SqMenuList L;没必要用指针。(SqMenuList *L这么用的时候,你需要使用malloc分配空间),然后把后面的L->都改成 L.(L点)
第二种改法是:
把SqMenuList *L;改成:
SqMenuList *L = (SqMenuList *)malloc(sizeof(SqMenuList));
//这种改法需要在main函数的最后释放空间,free(L);
修改如下,供参考:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxSize 100
typedef struct elem{
int id; //序号
char name[50];//名
int price; //价格
}ElemType; //菜单的数据元素结构体类型
typedef struct{
ElemType data[MaxSize];
int length;
}SqMenuList;
int main()
{
SqMenuList *L=(SqMenuList*)malloc(sizeof(SqMenuList)); //SaMenuList *L;
int i;
L->length=0;
(L->data[0]).id=1;
strcpy(L->data[0].name,"农家小炒肉");
(L->data[0]).price=58;
L->length++;
(L->data[1]).id=3;
strcpy(L->data[1].name,"非菜炒蛋");
(L->data[1]).price=22;
L->length++;
(L->data[2]).id=2;
strcpy(L->data[2].name,"番茄炒饭");
(L->data[2]).price=33;
L->length++;
for(i=0;i<L->length;i++)
printf("%2d,%-s,%3d\n",(L->data[i]).id,(L->data[i]).name,(L->data[i]).price);
//printf("%d,%s,%d",(L->data[0]).id,(L->data[0]).name,(L->data[0]).price);
return 0;
}