c - 为什么给数组结构赋值不起作用 C

标签 c arrays struct malloc

我正在尝试编写一个收集学生信息的程序。我正在使用一组学生(结构)

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 StudentStudent可能是两种不同的类型。 Student 来自您的 typedef,struct Student 来自

struct Student { /* ... */ };

所以你的函数应该是

void getStdData(Student *students)

为了更好地讨论这种情况,请考虑 this answer .

关于c - 为什么给数组结构赋值不起作用 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37634137/

相关文章:

C - MSVS 2013 "C2374: ' 名称': redefinition, 多重初始化”- 为什么?

c - 我的程序崩溃了,我不明白为什么

c - 为什么通过引用传递时 C 中结构的大小会增加

matlab - 在Matlab中初始化结构

c - 无法打印c中链表中的元素

ios - 线程 1 : Fatal error: Index out of range. 无法快速从数组中获取值,控制台显示它们不是空数组

python - 保存数组中每个时间戳之间的时间量

ios - 创建要在选择器 View 中使用的数字数组不起作用

c++ - 对结构的困惑

c - NULL 未声明的标识符