0

This is the program i'm trying to write:

#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);
    }
}

When i ran it, i encountered these errors:

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));
                  ^

What does it mean by: "‘student’ undeclared" when i already declared it in the void() function and what expression am i expected to put into my function at the specified place?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56

1 Answers1

0

The proper way to allocate the memory would be:

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

You didn't create a typedef, so you need to refer to it with the struct keyword.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67