这是我要编写的程序:
#include<stdio.h>
#include<malloc.h>
struct student {
char name[20];
float point;
};
void inputlst (struct student *sv, int n){
int i;
for(i = 0; i < n; i++){
puts("Name: "); gets((sv+i)->name);
puts("Point: "); scanf("%f", &(sv+i)->point);
}
}
void outputlst (struct student *sv, int n){
int i;
for(i = 0; i < n; i++){
puts((sv+i)->name); printf("%f", (sv+i)->point);
}
}
void main(){
struct student *classA;
int n;
printf("No. of students: ");
scanf("%d", &n);
classA = (student*)malloc(n*sizeof(student));
if(classA == NULL)
puts("Could not provide dynamic memory");
else{
inputlst(classA, n);
outputlst(classA, n);
}
}
当我运行它时,我遇到了这些错误:
1/error: ‘student’ undeclared (first use in this function)
classA = (student*)malloc(n*sizeof(student));
^~~~~~~
2/error: expected expression before ‘)’ token
classA = (student*)malloc(n*sizeof(student));
^
当我已经在void()函数中声明它以及我期望在指定的位置放入函数的表达方式时,"学生"未宣布"是什么意思?
分配内存的正确方法是:
struct student *students;
students = malloc(sizeof(struct student) * n);
您没有创建TypEdef,因此您需要使用struct
关键字引用。