Calling a function through a function pointer - dereference the pointer or not? What's the difference?

北慕城南 提交于 2019-11-29 10:34:29
sort_ptr( arr1, 5 );

and

(*sort_ptr)( arr2, 5 );

Both are correct. In fact, you can put as many asterisks you want and they are all correct:

(*****sort_ptr)( arr2, 5 );

The name of function decays to a pointer to a function. So dereferencing it repeatedly is going to produce the same pointer.

As far as the compiler is concerned, sort_ptr and (*sort_ptr) are identical. If sort_ptr really is a pointer, however, explicitly dereferencing it makes things a lot clearer for the reader. In general; there is one case where the fact that you can call a function directly on a pointer to function is useful: in templates, where it makes a pointer to function a functional object, which behaves exactly like a class with an operator()().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!