C语言为什么无法输出结果?

#include<stdio.h>
#define N 4
struct student
{
char *name;
int number;
int score;
};
int main()
{
typedef struct student stu;
stu arry[N];
int i;
for(i=0;i<N;i++)
{
printf("Input the student%d informaintion:\n",i+1);
scanf("%s",&arry[i].name);
scanf("%d",&arry[i].number);
scanf("%d",&arry[i].score);
}
for(i=0;i<N;i++)
{
printf("the student%d: %s %d %d\n",i+1,arry[i].name,arry[i].number,arry[i].score);
}
return 0;
}

段错误

student的name没有申请空间

两个地方需要修改

struct student
{
char name[10];//大小可根据需求自己改变
int number;
int score;
};

第二个地方

 scanf("%s",&arry[i].name);改为
  scanf("%s",arry[i].name);

谢谢采纳

追问

恩,改了可以输出!如果我想用指针指向我输入的名字,要怎么样定义啊?

追答

那你指向的那块地址需要申请内存。比如说一个数字或者malloc的一块内存

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-12-27
scanf("%s",&arry[i].name); scanf("%d",&arry[i].number); scanf("%d",&arry[i].score); }
你arry[i].name不要&号,因为你name是指针,你可以理解为name数组,这样好理解,呵呵
第2个回答  2013-12-27
struct student{char *name;int number;int score;};这儿应该有分号吗?

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网