0

Suppose func_ptr is a function pointer to the function test().Then we know that can invoke the function test() using this pointer as

   (*func_ptr)();

But I was told today that even (***func_ptr)() or (**********func_ptr)() ,ie with any number of * works.Why is it so?What is the reason.I was briefly told the reason in a comment but I just can't make sense of it.This is what I was told:

Well, when you dereference a function pointer, you get an expression of function type (technically a "function designator"). However, when used in most contexts, such a function expression will be implicitly converted back to a function pointer that points to itself. (This is similar to how in most contexts, an expression of array type will be implicitly converted to a pointer to its first element.) You can repeat this "loop" as many times as you want.

Can anyone explain in simple words in a more detailed manner?

4

1 回答 1

1

您需要考虑取消引用函数指针可能意味着什么。从技术上讲,它可以被赋予一个含义,一个函数指针指向代码,因此取消引用它会在指向的地址处提供机器代码指令。但这能给你带来什么?你打算如何处理指令?就此而言,在指令长度可变的机器(例如 x86 等 CISC 处理器)上应该发生什么?

显然,这些问题没有明确的答案。因此,C 语言设计者可以选择禁止取消引用函数指针。或者他们可以指定取消引用函数指针没有效果并产生完全相同的指针值。他们选择了后者。

因此,正如您所观察到的,任何数量的 * 都没有效果。明智的选择是不使用。

于 2013-05-05T12:24:16.047 回答