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;

Yes, they are the same.

If you compile that code the 2nd one is wrong.

If you compile that code the 2nd one is wrong.

Care to elaborate?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.