Code 1:
int size;
struct demo
{
int a;
};
int main()
{
scanf("%d",&size);
struct demo d[size];
return 0;
}
This code works fine.
Code 2:
int size;
struct demo
{
int a;
};
int main()
{
scanf("%d",&size);
return 0;
}
struct demo d[size];
This code shows an error:
error : variably modified 'd' at file scope
Why such error is coming in Code 2 whereas Code 1 runs fine?
In code 2, your array of structs resides in data segment which is by definition
A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. The size of this segment is determined by the values placed there by the programmer before the program was compiled or assembled, and does not change at run-time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With