gpt4 book ai didi

c - ‘)’ token 和变量未声明错误之前的预期表达式

转载 作者:行者123 更新时间:2023-11-30 16:38:01 27 4
gpt4 key购买 nike

这是我正在尝试编写的程序:

#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() 函数中声明它时,“‘student’未声明”是什么意思?我希望在指定位置将什么表达式放入我的函数中?

最佳答案

分配内存的正确方法是:

 struct student *students;
students = malloc(sizeof(struct student) * n);

您没有创建 typedef,因此需要使用 struct 关键字引用它。

关于c - ‘)’ token 和变量未声明错误之前的预期表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47625818/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com