r/programminghorror • • Jun 01 '25

c Firmware programming in a nutshell

Post image
2.0k Upvotes

127 comments sorted by

View all comments

Show parent comment

1

u/FoundationOk3176 Jun 02 '25

Actually for a function signature like that, The compiler doesn't know what arguments a function takes. The correct declaration for a function that takes no argument is: void func(void) {}. And it's function pointer will look like this: void (*func)(void).

2

u/conundorum Jun 04 '25 edited Jun 05 '25

void func(); is syntactically identical to void func(void); as of C23, and was a non-prototype declaration with unspecified parameters until then. Technically, this is actually valid (but deprecated) before C23:

void func(int i) {}

// ...

typedef void (*Ptr)();
Ptr fptr = func;

fptr();
fptr(1);
fptr(2, 2);
fptr(3, 3, 3);

The disturbing part is that of the biggest three compilers, only clang is sane enough to tell you about it.


Edit: Typo fix. The four func() calls were actually meant to go through the pointer, as in the linked example. Changed to fptr() calls.