int Add(int nX, int nY)
{
    return nX + nY;
}

int main()
{
    
    int (*pFcn)(int, int) = Add;
    int (*pFcn)(int, int) = &Add;
    cout << pFcn(5, 3) << endl; // add 5 + 3

    return 0;
}

my question is are both of these lines equivalent? or is one wrong and one right?

int (*pFcn)(int, int) = Add;
int (*pFcn)(int, int) = &Add;

They both compile and give the same results.

A function has its own unique memory address. The function's entry point is this address.

Therefore, you can use a pointer to a function to call the function which is essentially what you are doing in the second declaration. int (*pFcn)(int, int) = &Add; .

When the compiler assembles the code for a function, it assigns an entry point for that function or a specific memory address. Using the address of the function (&Add) produces the same result that the compiler will eventually come up with on its own: the address of the entry point.

my question is are both of these lines equivalent? or is one wrong and one right?

int (*pFcn)(int, int) = Add;
int (*pFcn)(int, int) = &Add;

They're both correct and functionally identical. The reason why is because you can only do two things with a function: call it and take its address. Any use that isn't a call (using the () operators) must be a request for the address.

Edited 5 Years Ago by Narue: n/a

This article has been dead for over six months. Start a new discussion instead.