编译,链接没问题,程序运行中出错,哪位大哥看看问题在哪?
// 调用函数这个写法,在输入第二个数据的时候,程序运行错了。在main中操作的写法是可以。# include <stdio.h>
# include <malloc.h>
struct Student
{
int age;
float score;
char name[10];
};
void InputStudent(struct Student ** st, int len)
{
int i;
*st = (struct Student *)malloc( len * sizeof(struct Student ) );
for (i=0; i<len; ++i)
{
printf("请输入第%d个学生的信息\n", i+1);
printf("age = ");
scanf("%d", &((*st[i]).age));
printf("score = ");
scanf(" %f", &((*st[i]).score));
printf("name = ");
scanf("%s", &((*st[i]).name));
getchar();
}
}
int main(void)
{
int len;
int i;
struct Student * pArr;
printf("请输入要输入学生信息的个数:\n");
scanf("%d", &len);
// pArr = (struct Student *)malloc( len * sizeof(struct Student) );
//输入学生信息
InputStudent(&pArr, len);
/* for (i=0; i<len; i++)
{
printf("请输入第%d个学生的信息\n", i+1);
printf("age =");
scanf("%d", &pArr[i].age);
printf("score = ");
scanf("%f", &pArr[i].score);
printf("name = ");
scanf("%s", &pArr[i].name);
}
*/
//输出学生信息
for (i=0; i<len; i++)
{
printf("第%d个学生的信息是:\n", i+1);
printf("age =%d\n",pArr[i].age);
printf("score = %f\n",pArr[i].score);
printf("name = %s\n", pArr[i].name);
}
return 0;
}
[ 本帖最后由 hyddpjh 于 2014-2-27 11:16 编辑 ]