array a[(foo)()] (help)

N

noname_is_me

int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
return a;
}


int main(void)
{
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.
 
A

Alex Vinokur

noname_is_me said:
int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
return a;
}


int main(void)
{
bar();
{
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.
 
J

Jens.Toerring

noname_is_me said:
int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;

There's no need for the address operator here.
return a;
}

int main(void)
{
unsigned int a[(foo)()];

You don't need the parentheses around 'foo'.
printf("%d\n",sizeof a);
return 0;
}
gcc does not give any compile time error. But the program crashes. why?

If you run gcc in c90-compliant mode it's going to warn you about
variable-size array you're using, which are only allowed in C99. And
another problem is that in the calculation of the size you use the
uninitialized pointer 'foo' as if it would point to a function,
which it doesn't ('foo' would only gets set in function bar(), but
that is never called).
Regards, Jens
 
A

Amit

noname_is_me said:
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.
 
R

Richard Bos

noname_is_me said:
int (*foo)(void);
int bar(void)
{
unsigned int a = 10;
foo = &bar;
return a;
}

int main(void)
{
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?

Array a has nothing to do with it. You're calling an uninitialised
function pointer; foo is never given a value.

Richard
 
R

Richard Bos

Amit said:
noname_is_me said:
int main(void)
{ bar(); /* added */
unsigned int a[(foo)()];
printf("%d\n",sizeof a);
return 0;
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.

This is right.
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.

This is wrong. ANSI C, these days, is ISO C99; at least, I'd be very
surprised if ANSI had not ratified C99. ANSI C89 and ISO C90 did not
allow the things you mention, that's true.

Richard
 
N

noname_is_me

Alex said:
I must call the function bar() since it initializes the function pointer
foo. Am I correct?
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top