As Umaiki has already said, you access memory you have never allocated.
In contrast to his answer, I provide a different approach:
First, this is how we define the struct
:
typedef struct {
int id;
unsigned int age;
} student;
after that, we can allocate the students array in the main
method like this:
student* a = malloc(4 * sizeof(student));
Now we can access the student at <index>
like so:
a[<index>].id = <value>;
a[<index>].age= <value>;
And lastly, here is a complete example of what (I think) you want to achieve, combing all the snippets I have shown above and including the call to free
(which is negligible in this case, because you exit directly thereafter, but it's something you should never forget):
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
unsigned int age;
} student;
int main() {
student* a = malloc(4 * sizeof(student));
a[0].id = 20;
a[0].age = 22;
a[1].id = 23;
a[1].age = 24;
a[2].id = 25;
a[2].age = 26;
a[3].id = 27;
a[3].age = 28;
for (int i = 0; i<4; i++)
{
printf("%d %d \n", a[i].id, a[i].age);
}
free(a);
return 0;
}