Function Pointers

Why function pointers are not dereferanced?

Questions by swatijagdale

Showing Answers 1 - 4 of 4 Answers

Because there is no "object" or "value" which you'd get by dereferencing a pointer to it. Functions are not objects. You cannot copy them, delete them, create them using "new", etc. They only exist as a collection of binary instructions and data, and the most one can do is to get the address of that collection and pass that address around.

  Was this answer useful?  Yes

It is possible to dereference a function pointer.
By doing so, v will get the address of the function it points to.

Try this code :

Code
  1. #include <iostream>

  2. #include <tchar.h>

  3.  

  4. using namespace std;

  5.  

  6. int sum(int a, int b)

  7. {

  8.             return a+b;

  9. }

  10.  

  11. int _tmain(int argc, _TCHAR* argv[])

  12. {

  13.             int (* ptr)(int, int) = sum;

  14.  

  15.             cout << *ptr << endl;

  16.  

  17.             return 0;

  18. }


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions