简体   繁体   English

函数名称和函数指针

[英]Function Name and Pointer to function

Did the name of the function represent an pointer to this function like array? 函数名称是否代表指向该函数的指针(例如数组)?

If I declare an function as follow void fu (void); 如果我将函数声明为void fu (void); and an array of pointer to function like that void(*ptr_fn[8])(void); 以及指向该函数的指针数组,例如void(*ptr_fn[8])(void); so can I do that ptr_fn[0] = fu; 所以我可以做ptr_fn[0] = fu;

No, and the name of an array does not represent a pointer, either. 不,数组的名称也不代表指针。

The name represents the function, but there's an implicit conversion when you use the function name in an expression. 名称代表函数,但是在表达式中使用函数名称时会有隐式转换。

See 4.3 Function-to-pointer conversion: 请参见4.3函数到指针的转换:

An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function. 函数类型T的左值可以转换为“指向T的指针”的前值。结果是指向函数的指针。

Yes. 是。 It is also equivalent to: 它也等效于:

ptr_fn[0] = &fu ;

ie the & is optional. &是可选的。

Equally when calling the function through the pointer 同样地,通过指针调用函数时

ptr_fn[0]() ;

is equivalent to: 等效于:

(*ptr_fn[0])() ;

The use of the & and * operators serves to emphasise perhaps that you are dealing with a function-pointer, and so aids maintenance and comprehension by humans, but has no effect on the compiler's code generation. 使用&*运算符可能会强调您正在处理一个函数指针,因此有助于人类进行维护和理解,但对编译器的代码生成没有影响。

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

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