4 ms·
There’s no excuse for C not to have some syntactic sugar around function pointers at this point. They’re miserable to use.
by jimbob45 2y ago
There’s no excuse for C not to have some syntactic sugar around function pointers at this point. They’re miserable to use.
- ajb 2y agoIt does. You don't need to use & on a function or * on a function pointer, this happens automatically (see https://stackoverflow.com/questions/7518815/function-pointer-automatic-dereferencing https://stackoverflow.com/questions/7518815/function-pointer... ). I suppose the : operator from lua would be useful.
- fuhsnn 2y agoWith "typeof" standardized in C23 it kinda does the job: int(* fnptr_name)(int param) becomes typeof(int(int param))* fnptr_name There is a recent proposal to update standard headers to this style: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3450.txt https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3450.txt
- debugnik 2y ago-void (*signal(int sig, void (*func)(int)))(int); +typeof(void (int)) *signal(int sig, typeof(void (int)) *func); Much better honestly. The original took me a few reads to understand, until I noticed the outer (int) is part of the return type.
- jstimpfle 2y agoC syntax becomes hard to read once you nest function declarations (i.e. functions taking functions, taking functions...). But that's actually the case for most common syntaxes. Haskell type syntax is an exception, but that's a language where you're using lots of higher-order functions. For the most common use cases C syntax is quite ergonomic once you've learned the principle. // declare func as normal function void func(ARGUMENTS); // declare (*func) as normal function: func is a pointer to a normal function void (*func)(ARGUMENTS); // can also declare a type name instead of a function typedef void functype(ARGUMENTS); // and then use that to declare function-pointer functype *func; // can also declare a function pointer type directly (I don't normally do this) typedef void (*funcptrtype)(ARGUMENTS); funcptrtype func; // declare function pointer