scanf的问题,到底要不要加“&”?
在yousikyzll的帖子http://topic.csdn.net/u/20120824/22/5d78218c-5c1f-46ea-a811-0d31f849fe3a.html?seed=359067998&r=79506152 中提到的问题大家对scanf函数中要不要加&有不同意见。原帖作者结贴太早也没讨论个结果。我加上&能正常运行,不加&的话出错。
在这里要不要加?希望各位大神能给个详细的解释。
#include "stdafx.h"
typedef struct{
float chi;
float math;
float eng;
float phy;
float chem;
}score_1;
typedef struct{
int num;
char name[20];
char sex;
int age;
score_1 score;
}student;
student stu[10];
int main(int argc, char* argv[])
{
int i;
for(i=0;i<10;i++)
{
printf("请输入学生信息:\n");
scanf("%d,%s,%c,%d,%f,%f,%f,%f,%f",&stu[i].num,&stu[i].name,&stu[i].sex,&stu[i].age,&stu[i].score.chi,&stu[i].score.math,&stu[i].score.eng,&stu[i].score.phy,&stu[i].score.chem);
}
printf("Hello World!\n");
return 0;
}
数据输入如下:
0021,liqiang,m,20,85,78,89,56,78(回车)......
[解决办法]
scanf 函数要求“第二个”参数是指针
[解决办法]
肯定要加&
[解决办法]
1、已经是指针的不加,比如char name[20]中的name;
2、不是指针的要加,比如float math中的math。
[解决办法]
[解决办法]
是指针的不加
不是的取地址