I tried using struct variables in global scope and local scope in C. When used in local scope ( inside main() ) it gets executed properly.
#include<stdio.h>
struct student{
char *name;
int age;
float avg;
};
struct student s1, s2; #throws error
int main(){
// struct student s1, s2; #gets executed
printf("Enter name, age, avg\n");
scanf("%s %d %f ",s1.name,&s1.age,&s1.avg);
printf("The values are \n");
printf("\nName : %s",s1.name);
printf("\nage : %d",s1.age);
printf("\navg : %f",s1.avg);
}
What is the reason if I use struct student s1, s2; above main() function it throws error ? If given in local scope it gets executed ?
I want to know the difference of What is happening to the above mentioned case. I also doubt that is it because of pointer variable declared inside struct which causes error?
Error is: