结构体数组使用,46行,报错无法将参数 1 从“Student (*)[3]”转换为“Student *[]”
“void printInfo(Student *[])”: 无法将参数 1 从“Student (*)”转换为“Student *[]”#include<winuser.inl>
#include<stdio.h>
#include <cstdlib>
//有一个结构体变量stu,内含学生学号、姓名和3门课程的成绩。要求在main函数中赋予值,
//在另一函数print中将它们输出。今用结构体变量作函数参数
int number_of_stu = 3;
struct Student
{
int num;
char name;
float score;
}stu;
void printInfo(struct Student *stu[])
{
for (int i = 1; i < number_of_stu + 1; i++)
{
printf("********print %d student information:*********\n", i);
printf("num:%d\n", stu -> num);
printf("name:%s\n", stu->name, 20);
printf("score:%f\n", stu->score);
printf("\n");
}
}
int main()
{
//输入数据
for (int i = 1; i < number_of_stu + 1; i++)
{
printf("enter %d student number:", i );
scanf_s("%d", stu.num);
printf("enter %d name:", i);
scanf_s("%s", stu.name,20);
printf("enter %d student number:", i);
scanf_s("%f", stu.score);
}
//将结构体作为参数传入函数中进行打印
printInfo(&stu);
return 0;
} 你这不是典型的脚踩两只船嘛,数组与指针二选一即可。你定义printInfo(struct Student* stu)或者定义printInfo(struct Student stu[]) 改好的代码
#include<stdio.h>
#include <cstdlib>
//有一个结构体变量stu,内含学生学号、姓名和3门课程的成绩。要求在main函数中赋予值,
//在另一函数print中将它们输出。今用结构体变量作函数参数
int number_of_stu = 3;
struct Student
{
int num;
char name;
float score;
} stu;
void printInfo(struct Student stu[]) // 有错
{
for (int i = 1; i < number_of_stu + 1; i++)
{
printf("********print %d student information:*********\n", i);
printf("num:%d\n", stu . num) ; // 有错
printf("name:%s\n", stu . name) ; // 有错
printf("score:%f\n", stu . score) ; // 有错
printf("\n") ;
}
}
int main()
{
//输入数据
for(int i = 1; i < number_of_stu + 1; i++) {
printf("enter %d student number : " , i) ;
s_scanf("%d", & stu . num) ; // 有错
printf("enter %d name : " , i) ;
s_scanf("%s", stu . name , 20) ;
printf("enter %d student score : " , i);
s_scanf("%f", & stu . score) ; // 有错
}
//将结构体作为参数传入函数中进行打印
printInfo(stu) ; // 有错
return 0;
}
页:
[1]