Following is a program for initializing members of a structure in c
struct stack
{
int *a;
int top;
int size;
}s;
void init()
{
const int size =10; /*s.size=10;*/ /*const int s.size=10*/
s.a=(int*)malloc(size*sizeof(int));
s.top=0;
}
int main()
{
init();
printf("Initialization done !\n");
return 0;
}
Q1 : In init
method instead of const int size=10
when I wrote s.size=10
, I got an error "size was not declared in scope " , but I have already declared size
in stack
struct .I was able to initialize top
in the same manner Then why the error ?
Q2 : In init
method I am getting correct output with const int size=10
. I am confused , in this statement how are we able to access size
member of the struct stack
without using a struct variable , shouldn't it be const int s.size=10
?