Hi guys,
Here is an example about pointer to function (was mentioned in the tutorial):
// pointer to functions
#include <iostream>
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
-----------------------------------
When I debugged it, I tracked after the values:
functocall
addition
subtraction
Which supposed to be the pointers for these functions.
The first time we call the function "operation":
m = operation (7, 5, addition);
The value of "functocall" should be the same as "addition", but it's not what I got.
What I got was:
functocall = 0x0041127b
addition = 0x004117a0
Yes (ish). functiontocall is a pointer to a function. It points to the address in the
Function Address Table (let's call it FAT) that holds the actual address of the addition function.
In your case, the address that it points to in the FAT is 00411050. (if you open a memory window and check that address you will see that it holds 004117b0 ).
The watch window and cout behaves differently
The Watch window simply dereferences the functocall pointer and gets 00411050. For Addition the watch windows will show the actual address which in this case is 004117b0
In the case of cout, the functocall pointer is fully dereferenced to get the actual address of the function.
So cout << functocall << " " << addition shows the same value
**Please note: feel free to jump in and correct me - is the FAT really a jump table??**