简体   繁体   English

在函数调用中使用多个星号有什么用?

[英]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++? 为什么在C和C ++中都允许这个东西?

One of the standard conversions, in both C and C++, is the function-to-pointer conversion; C和C ++中的标准转换之一是函数到指针的转换; 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相当于&foo
  • *foo is equivalent to *(&foo) , or foo *foo相当于*(&foo)foo
  • **foo is eqivalent to **(&foo) , or *foo , or foo **foo**(&foo) ,或*foofoo等效

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++? 为什么在C和C ++中都允许这个东西?

I can't speak for C++, but for C at least a function designator is converted to a pointer: 我不能代表C ++,但对于C,至少将函数指示符转换为指针:

6.3.2.1 - 4 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 ''. 除非它是sizeof运算符或一元&运算符的操作数,否则具有类型''函数返回类型''的函数指示符将转换为具有类型'' 指向函数返回类型 '的指针的表达式。

Applying the indirection operator yields a function designator: 应用间接运算符会产生一个函数指示符:

6.5.3.2 - 3 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. 在我看来,这样做很少或没有用。

Because the * operator expects an address value. 因为*运算符需要一个地址值。 And whenever a value is expected (as opposed to an object or function glvalue), the lvalue to rvalue, function to pointer and array to pointer conversions are applied on an operand. 每当一个值出现时(与一个对象或函数glvalue相反),rvalue的左值,指针和数组到指针转换的函数都应用于一个操作数。 So the dereferenced function immediately again converts to a pointer when again dereferenced. 因此,当再次取消引用时,解除引用的函数立即再次转换为指针。

These all either read values from objects or produce a pointer value that refers to the beginning of an array or function respectively. 这些都是从对象读取值或产生分别引用数组或函数开头的指针值。

These rows of dereferences have no purpose other than for the lulz of it. 除了它的lulz之外,这些dereferences行没有任何目的。

The way i understand it is 我理解的方式是

* is a pointer to a memory address
& is the value at the Memory address

*foo means pointer to foo memory address
**foo means *(*foo) *(foo memory address) This is a different value from *foo

it continues like that... 它继续这样......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM