Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Are these two function pointer declarations equivalent?

+3
−0

Say I have two functions:

FILE* get_input(const char fname[static 1]);
FILE* get_output(const char fname[static 1]);

And I wish to declare a function pointer and assign it the result of some processing which shall be called later.

Are these two declarations:

int foo(FILE *, FILE *);

int (*operation)(FILE *, FILE *);
typeof (foo) *op;

equivalent? Or is there any difference between the types of operation and op? If so, how do I properly declare a function pointer with typeof?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

What is foo? (2 comments)

1 answer

+6
−0

Normally, whenever a function designator (a function's name) is used in an expression, it decays to a function pointer to that function.

typeof is one of those exceptions to the "decay" rules of C (C23 6.3.2.1 §4), so if you give it a function type as operand, the result is a function type and not a function pointer type.

So it is now possible to write obscure declarations like:

void a (void);
typeof(a) b;

And that's equivalent to:

void a (void);
void b (void);

So therefore given some function int foo (FILE*, FILE*), thentypeof(foo)* will yield a function pointer of the type int (*) (FILE*, FILE*). Meaning that operation and op in your example would indeed be equivalent.

I would however not recommend this style since it is obscure and also not backwards-compatible. The clearest style IMO, compatible all the way back to C90 is this:

typedef int foo_t (FILE*, FILE*); // typedef a function not a pointer
...
foo_t* fp1;       // function pointer
foo_t* const fp2; // function pointer residing in ROM

This makes function pointer syntax 100% in sync with object pointer syntax.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »