I can't think of any practical use of multiple asterisks in the function call:
void foo(int a, char b) { } int main(void) { (**************foo)(45, 'c'); //or with pointer to function: void (*ptr)(int, char) = foo; (******ptr)(32, 'a'); }
Why is this thing allowed both in C and C++?
It declares a pointer to a char pointer.
A pointer in C and C++ programming is a variable that points to an address of another variable and not its value. When creating a pointer, use an asterisk (*); when determining the address of the variable, the ampersand (&), or the address-of operator, will display this value.
One of the standard conversions, in both C and C++, is the function-to-pointer conversion; when a function name appears in an expression, it can be converted into a pointer to that function. So:
foo
is equivalent to &foo
*foo
is equivalent to *(&foo)
, or foo
**foo
is eqivalent to **(&foo)
, or *foo
, or foo
and so on.
This means that you can legally add as many *
as you like before a function name without changing its meaning. There's no reason to do that, though.
Why is this thing allowed both in C and C++?
I can't speak for C++, but for C at least a function designator is converted to a pointer:
6.3.2.1 - 4
A function designator is an expression that has function type. Except when it is the operand of the sizeof operator or the unary & operator, a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’.
Applying the indirection operator yields a function designator:
6.5.3.2 - 3
The unary * operator denotes indirection. If the operand points to a function, the result is a function designator
So no matter how many times you apply the indirection operator you'll get the same thing: a function designator that's immediately converted to a pointer.
In my opinion there's little or no use in doing this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With