我正在尝试编写一个收集学生信息的程序。我正在使用一组学生(结构)
typedef struct {
char name[50];
struct Course* course;
}Student;
在我的 main() 中我这样做了
Student* stds = (Student*) malloc(app.std_cnt * sizeof(*stds) );
getStdData(stds);
这是 getStdData 函数
void getStdData(struct Student *students){
int i;
char name[50];
Student std;
printf("\n");
for(i = 0; i < app.std_cnt; i++){
printf("std [%i] name : ",i+1);
scanf("%s",&name);
strcpy(std.name,name);
students[i] = std;
}
}
当我编译时,我得到
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23026 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
gpa.c
gpa.c(124): error C2440: '=': cannot convert from 'Student' to 'Student'
谁能告诉我我做错了什么? 为什么要提示学生向学生的转变?它们不是同一类型吗?
最佳答案
在C中,struct Student
和Student
可能是两种不同的类型。 Student
来自您的 typedef,struct Student
来自
struct Student { /* ... */ };
所以你的函数应该是
void getStdData(Student *students)
为了更好地讨论这种情况,请考虑 this answer .
关于c - 为什么给数组结构赋值不起作用 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37634137/