5 ms·
That is correct. int x, *p, arr[5], fn(), (*pfn)(); Using x, or dereferencing p, or subscripting the array arr, or declaring a function that can be called w
by nitrix 9mo ago
That is correct.
int x, *p, arr[5], fn(), (*pfn)();
Using x, or dereferencing p, or subscripting the array arr, or declaring a function that can be called with fn, or dereferencing the function pointer pfn then calling it, all these things would produce an int.
It's the intended way to read/write declarations/expressions. As a consequence, asterisks ends up placed near the identifiers. The confused ones will think it's a stylistic choice and won't understand any of this.
- saagarjha 9mo agoOf course, the correct way to use a function pointer is to call it.
- nitrix 9mo agoYes, the () operator dereference function pointers automatically for you for convenience. There's also the surprise that you can infinitely dereference function pointers as they just yield you more function pointers.
- korianders 9mo agoOne baffling thing I see people do with typedefing function pointers is insisting on adding in the pointer part in the typedef which just complicates and hides things. If you want to typedef a function pointer, make a completely ordinary function declaration, then slap 'typedef' at the beginning, done. This does require you to do "foo_func *f" instead of "foo_func f" when declaring variables, but that is just clearer imo. typedef int foo_func(int); // nice typedef int (*foo_func)(int); // why?
- cryptonector 9mo agoWhy do you need the `*` to be part of every variable/member declaration?
- any1 9mo ago> It's the intended way to read/write declarations/expressions. As a consequence, asterisks ends up placed near the identifiers. You know you don't always have to use things as they were intended? > The confused ones will think it's a stylistic choice and won't understand any of this. Well, I've written it both ways, and the compiler never seems to mind. :) Maybe I should start putting space on both sides of the asterisk; seems like it would be a good way to annoy even more people.
- pwdisswordfishy 9mo agoBlame Stroustrup. https://www.stroustrup.com/bs_faq2.html#whitespace https://www.stroustrup.com/bs_faq2.html#whitespace