关于C语言动态分配结构体数组

raymondguo008 raymondguo008 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
想要动态输入学生信息,但是运行时候总出错。
不知道哪里错了。
望高手指点!


#include <stdio.h>

int len;
int i,j;

struct stu {
int num;
char *name;
char sex;
};

struct stu * create() {
struct stu *ps;

printf("Input total students!\n");
scanf("%d",&len);
ps=(struct stu *)malloc(len*sizeof(struct stu));
for(i=0;i<len;i++) {
scanf("%d%s%c\n",&ps[i].num,&ps[i].name,&ps[i].sex);
}
return ps;
}

void myPrint(struct stu *ps) {
for(j=0;j<len;j++) {
printf("%d %s %c\n",ps[j].num,ps[j].name,ps[j].sex);
}
}

main() {
struct stu *p;
p=create();
myPrint(p);
getch();
return 0;
}
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
raymondguo008
C语言 70,039 成员 243,237 内容