noname_is_me wrote:
int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
return a;
}
int main(void)
{
bar(); /* added */ unsigned int a[(foo)()];
printf("%d\n",sizeof a);
return 0;
}
gcc does not give any compile time error. But the program crashes. why?
Array 'a' can not
be decleared with size 10 * 4 size? Please help.
I am a beginner in C so i am not sure if i am right or wrong.
When the main() is called,at that time foo doesnt have the address of
bar(),so obviously it will try to go to some garbage value contained in
foo and try to execute it and thus results in a crash.
As for gcc,execute bar function before your declaration.
As ANSI C doesn't allow "variable-size array".Moreover the solution here
is also not ANSI C complaint.It gives error: ISO C89 forbids mixed
declarations and code.