Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use of multiple asterisks in the function call?

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++?

like image 730
Quentin Avatar asked Aug 30 '12 07:08

Quentin


People also ask

What does two asterisks mean in C?

It declares a pointer to a char pointer.

What is the asterisk used for in C++?

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.


2 Answers

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.

like image 153
Mike Seymour Avatar answered Sep 25 '22 16:09

Mike Seymour


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.

like image 35
cnicutar Avatar answered Sep 23 '22 16:09

cnicutar