小编Med*_*nga的帖子

为什么为数组结构赋值不工作C.

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

typedef struct {
  char name[50];
  struct Course* course;
}Student;
Run Code Online (Sandbox Code Playgroud)

在我的主要()我这样做了

Student* stds = (Student*) malloc(app.std_cnt * sizeof(*stds) );

getStdData(stds);
Run Code Online (Sandbox Code Playgroud)

这是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;

 }
}
Run Code Online (Sandbox Code Playgroud)

当我编译我得到

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'
Run Code Online (Sandbox Code Playgroud)

谁能告诉我我做错了什么?为什么抱怨学生转学?他们不是同一类型?

c arrays malloc struct

-2
推荐指数
1
解决办法
71
查看次数

标签 统计

arrays ×1

c ×1

malloc ×1

struct ×1