Page 1 of 1

C Standard question

Posted: Sat Jun 24, 2006 5:33 am
by Midas
Reading the C standard itself at some point is on my TODO list, but until such times as I do:

Does the C standard mandate that simply naming a function must return the address of that function, or should it be prefixed with an ampersand to remain strictly standards-compliant?

i.e. are the following equally correct?

Code: Select all

void (*fptr)(void) = foo;
void (*fptr)(void) = &foo;

Re:C Standard question

Posted: Sat Jun 24, 2006 5:42 am
by Kemp
I assume that if you're asking the question it's because you already noticed this occurring, but just so everyone's working from the same page, most compilers will treat the two different forms as effectively the same thing.

As to what the standards say, Solar's probably your guy for that.

Re:C Standard question

Posted: Sat Jun 24, 2006 6:37 am
by bluecode
afaik only the second is standard compliant.

Re:C Standard question

Posted: Sat Jun 24, 2006 7:36 am
by Midas
@Kemp - It is indeed. I've always defaulted to the latter, because it seemed more logical to me. I'm using GCC which definitely interprets the two as the same, but if it doesn't have any obligation to do so... As for Solar: that's why I'm posting here. ;D

@bluecode - Thanks, that's what I thought would be the case, but there are a couple things that differ from what might be expected (e.g. when assigning a pointer the value of a void pointer, no cast being required) so I thought I'd ask.

Thanks, both. :D

Re:C Standard question

Posted: Sun Jun 25, 2006 10:29 pm
by Solar
Hmmm... my real strength lies with chapter 7 of the standard (i.e., the library specs), but I've browsed through it regarding your question... as far as I can see, [tt]()[/tt] is defined as a postfix operator on any expression that has the type "pointer to function". That would mean that the function name alone (without &) is legit. That's also what I would have said by "instinct".

Re:C Standard question

Posted: Mon Jun 26, 2006 8:38 am
by Midas
Right... I follow. Thanks, Solar, for a definitive answer. :D