69,369
社区成员
发帖
与我相关
我的任务
分享
#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);
}
#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);
struct student
{
char *name;
int score;
};