Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/12192368/whats-the-use-of-multiple-asterisks-in-the-function-call

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...