I'm doing a simple student database program exercise and I'm unsure of how to initialise an array of structures. I'm trying to initialize the first 3 elements of the array stdt[] with values known at compile-time, and then the next 3 students' information will be populated from user input. When I compile I get the error:
lab7.c: In function ‘main’:
lab7.c:16:9: error: expected expression before ‘{’ token
stdt[0]={"John","Bishop","s1234","Inf",'m',18};
^
lab7.c:17:9: error: expected expression before ‘{’ token
stdt[1]={"Lady","Cook","s2345","Eng",'f',21};
^
lab7.c:18:9: error: expected expression before ‘{’ token
stdt[2]={"James","Jackson","s33456","Eng",'m',17};
^
How can I do this correctly?
Here is the code so far:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char *name;
char *surname;
char *UUN;
char *department;
char gender;
int age;
} student_t;
int main() {
int i;
student_t stdt[6];
stdt[0]={"John","Bishop","s1234","Inf",'m',18};
stdt[1]={"Lady","Cook","s2345","Eng",'f',21};
stdt[2]={"James","Jackson","s33456","Eng",'m',17};
for(i=3;i<6;i++) {
printf("First name: \n");
scanf("%s",stdt[i].name);
printf("Last name: \n");
scanf("%s",stdt[i].surname);
printf("UUN: \n");
scanf("%s",stdt[i].UUN);
printf("Department: \n");
scanf("%s",stdt[i].department);
printf("Gender (m/f): \n");
scanf("%c",stdt[i].gender);
printf("Age: \n");
scanf("%d",stdt[i].age);
}
return 0;
}