帮忙看段代码,怎么数据写不进结构体数组啊........急急!!

sun31009 2008-06-15 11:45:18
#include<stdio.h>

struct student
{
char *name;
int score;
};

void main()
{
struct student stu[10],*p;
p=stu;
int i;
while(p<stu+10)
{
printf("请输入姓名:");
scanf("%s",&p->name);
fflush(stdin);
printf("请输入分数:");
scanf("%d",&p->score);
printf("%s,%d",p->name,p->score);
p++;
}
for(i=0;i<10;i++)
printf("第%d个学生信息:姓名 %s\t成绩 %d\n",i+1,stu[i].name,stu[i].score);
}

...全文
113 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
visir 2008-06-16
  • 打赏
  • 举报
回复
int main()
{
char *p;
scanf("%s", &p);

return 0;
};
// 这样的代码已经千百次的使千百个系统崩溃,使千百个程序员花千百倍于写代码的时间来找出这个错误
stars_eyes 2008-06-16
  • 打赏
  • 举报
回复
#include <stdio.h> 

struct student
{
char *name; //改为char name[80]; 原语句声明指针变量name,但是无明确指向,容易出问题;用字符数组,可以明确地分配内存
int score;
};

void main()
{
struct student stu[10],*p;
p=stu;
int i;
while(p <stu+10)
{
printf("请输入姓名:");
scanf("%s",&p->name); //改为scanf("%s",p->name); 以字符串形式进行输入,数组名就是地址,不需要再次取地址
fflush(stdin);
printf("请输入分数:");
scanf("%d",&p->score);
printf("%s,%d",p->name,p->score);
p++;
}
for(i=0;i <10;i++)
printf("第%d个学生信息:姓名 %s\t成绩 %d\n",i+1,stu[i].name,stu[i].score);
}
HelloDan 2008-06-16
  • 打赏
  • 举报
回复

#include <stdio.h>

struct student
{
char name[20]; //////////////
int score;
};

int main()
{
struct student stu[10],*p;
p=stu;
int i;
while(p <stu+10)
{
printf("请输入姓名:");
scanf("%s",&p->name);
fflush(stdin);
printf("请输入分数:");
scanf("%d",&p->score);
printf("%s,%d",p->name,p->score);
p++;
}
for(i=0;i <10;i++)
printf("第%d个学生信息:姓名 %s\t成绩 %d\n",i+1,stu[i].name,stu[i].score);

return 0;
}






printf("请输入姓名:");
p->name = new char[32];
scanf("%s",&p->name)
//////////////////
delete [] p->name;



printf("请输入姓名:");
p->name == (char*) malloc (32);;
scanf("%s",&p->name)
////////////////// process the data
free(p->name);







dbger 2008-06-16
  • 打赏
  • 举报
回复
也可以在输入的时候new.

printf("请输入姓名:");
p->name = new char[32];
scanf("%s",&p->name)
dbger 2008-06-16
  • 打赏
  • 举报
回复
student的name指针没有分配内存。

struct student
{
char name[32];
int score;
};
xujiie 2008-06-16
  • 打赏
  • 举报
回复
学习一下
帅得不敢出门 2008-06-16
  • 打赏
  • 举报
回复
struct student
{
char *name; //传说中的野指针.
int score;
};
K行天下 2008-06-16
  • 打赏
  • 举报
回复

struct student
{
char *name;
int score;
};

没有分配内存就妄图进行写入将导致不可预料的错误

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧