C Standard question

Programming, for all ages and all languages.
Post Reply
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

C Standard question

Post 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;
Regards,
Angus [Óengus] 'Midas' Lepper
Kemp

Re:C Standard question

Post 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.
bluecode

Re:C Standard question

Post by bluecode »

afaik only the second is standard compliant.
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

Re:C Standard question

Post 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
Regards,
Angus [Óengus] 'Midas' Lepper
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C Standard question

Post 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".
Every good solution is obvious once you've found it.
Midas
Member
Member
Posts: 140
Joined: Sat Jun 24, 2006 4:40 pm
Location: Falkirk, Scotland
Contact:

Re:C Standard question

Post by Midas »

Right... I follow. Thanks, Solar, for a definitive answer. :D
Regards,
Angus [Óengus] 'Midas' Lepper
Post Reply