Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected expression before 'struct'

Tags:

c

pointers

struct

I'm getting the error "expected expression before 'struct'" on the the first line in the function allocate() below. I cant figure out why.

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;
}
like image 712
Tanner Avatar asked Dec 20 '22 07:12

Tanner


1 Answers

You may want to write

sizeof(struct student)

instead of

sizeof struct *student
like image 185
Krishnabhadra Avatar answered Jan 04 '23 23:01

Krishnabhadra