C语言 'struct' 之前的预期表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16028343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
expected expression before 'struct'
提问by Tanner
I'm getting the error "expected expression before 'struct'" on the the first line in the function allocate() below. I cant figure out why.
我在下面的函数allocate() 的第一行收到错误“'struct' 之前的预期表达式”。我想不通为什么。
I should say that I've been tasked to make this code work with the structure / function headers provided.
我应该说我的任务是使这段代码与提供的结构/函数头一起工作。
Any help is much appreciated!
任何帮助深表感谢!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <time.h>
struct student{
int id;
int score;
};
struct student* allocate(){
/*Allocate memory for ten students*/
struct student *stud = malloc(10 * sizeof struct *student);
assert (stud !=0);
return stud;
}
void generate(struct student *students){
/*Generate random ID and scores for ten students, ID being between 1 and 10, scores between 0 and 100*/
srand(time(NULL));
// Generate random ID's
int i;
for(i=0; i<10; i++){
students[i].id = rand()*10+1;
}
//Generate random scores
for(i=0; i<10; i++){
students[i].score = rand()*10+1;
}
}
void output(struct student* students){
//Output information about the ten students in the format:
int i;
for(i=0; i<10; i++){
printf("ID-%d Score-%d\n", students[i].id, students[i].score);
}
}
void summary(struct student* students){
/*Compute and print the minimum, maximum and average scores of the ten students*/
int min = 100;
int max = 0;
int avg = 0;
int i;
for(i=0; i<10; i++){
if(students[i].score < min){
min = students[i].score;
}
if(students[i].score > max){
max = students[i].score;
}
avg = avg + students[i].score;
}
avg = avg/10;
printf("Minimum score is %d, maximum score is %d, and average is %d.", min, max, avg);
}
void deallocate(struct student* stud){
/*Deallocate memory from stud*/
free (stud);
}
int main(){
struct student *stud = NULL;
/*call allocate*/
stud = allocate();
/*call generate*/
generate(stud);
/*call output*/
output(stud);
/*call summary*/
summary(stud);
/*call deallocate*/
deallocate(stud);
return 0;
}
回答by Krishnabhadra
You may want to write
你可能想写
sizeof(struct student)
instead of
代替
sizeof struct *student
回答by autistic
Your problem is in sizeof struct *student. When using the sizeofoperator on a typename, you need to parenthesize the typename. In addition, as Jonathan Leffler identified in the comments to this answer, the placement of *in struct *studentis erroneous, and the use of struct student *would be incorrect in the context of this code. Perhaps you meant: sizeof (struct student).
你的问题在sizeof struct *student. 在类型名上使用sizeof运算符时,您需要将类型名括起来。此外,正如 Jonathan Leffler 在对此答案的评论中所指出的,*in的放置struct *student是错误的,并且在struct student *此代码的上下文中使用是不正确的。也许你的意思是:sizeof (struct student)。
Alternatively, you could use sizeofon an expression, and you won't need the parenthesis. This would be preferable, because if you choose to change the type of studthen you won't need to replace an extra typename when you do so: struct student *stud = malloc(10 * sizeof *stud);
或者,您可以sizeof在表达式上使用,并且不需要括号。这将是可取的,因为如果您选择更改的类型,stud那么您将不需要在这样做时替换额外的类型名:struct student *stud = malloc(10 * sizeof *stud);

