IDE: **MS Visual Studio 2008 Professional
**Compiler:
Unknown
First off let me thank any of you who take the time to read this and reply back.
I'm having a ridiculous amount of trouble returning a pointer from a function and then using that returned pointer to change the pointer-pointer in main(). I can't describe how much I dislike running here for help as I don't like wasting other peoples time. I shouldn't have a problem with this pointer stuff at my level and I find it embarresing.

The posted program here is a tiny snippet where i'm trying to isolate this function return problem. In the larger program PntrFunc() is actually dealing with a linked list and adds a node whilst main() calls it in a loop. The main objective here is to use the pointer-pointer to remember the last element of the list so it can be given to PntrFunc() and it can add the next link instead of repeatadly working on the list head.

The return value assignment in main should only update the pointer-pointer. The only thing that should alter the list is PntrFunc().

int* PntrFunc(int** itemGiven)
{
    int *newInt = new int;
    *newInt = 500 + **itemGiven;
    return newInt;
}


    int main()
    {

        int theVar = 50;
        int* pntr = &theVar;

        int** mPntr = &pntr;

        int theVar2 = 10;
        int* pntr2 = &theVar2;

        mPntr = &PntrFunc(&(*mPntr));

        return 0;
}

Upon compilation MS VS 2008 gives: error C2102: '&' requires l-value on the line:

mPntr = &PntrFunc(&(*mPntr));

I've googled for many an hour this error and it's either regarded as a bug in the IDE/Compiler or a genuine invalid c++ operation. In the latter results I fail to understand what's being with the '&' needing to be on the left of '='.

The funny thing is I get no issues when updating the pointer-pointer to look at a local pointer in main. So when I:

mPntr = &pntr2;

it works with no issues.

Please, save my sanity. It works as intended when I assign a local pointer to pointer-pointer but it falls flat on its face trying to assign a returned pointer to the pointer-pointer?

One issue is here: &(*mPntr)
The other is here: &PntrFunc(&(*mPntr))

You can't get the location of *mPntr, because that's an expression, so your code doesn't make any sense. It would be like if you were to try to do this: int* ptr = &(a + b); It's obvious to see why --- the expression (a+b) doesn't have a permanent location, so using the ampersand on it serves no purpose. You can only get the location of an actual variable, such as theVar2.

Likewise, you can't get the location of a function call, that also doesn't make any sense. A function call also has no permanent location.

int main()

{
int theVar = 50;
int* pntr= &theVar;
int** mPntr = &pntr;
int theVar2 = 10;
int* pntr2 = &theVar2;
pntr=(PntrFunc(mPntr));
mPntr=&pntr;
return 0;

Thanks for taking the time to read and reply.

What you write makes sense when I read it and yet my mind is still tangled around this pointer-pointer i've created. I didn't realise I was trying to get the address of the function instead of the returned pointers address.

What I was trying to do with

    &(*mPntr)

is just pass int pntr* to the function instead of the entire pointer-pointer.

I was trying to get the memory address of the returned pointer so I could change the pointer being pointed to by the pointer-pointer instead of the data being pointed to. I don't want int pntr* to be changed. I want mPntr to point to the returned pointer.

So, thanks to your feedback i've managed to "get it working" but it still seems somehow "wrong" in the way i'm doing it.

So, PntrFunc() now returns a pointer-pointer and i'm making a pointer-pointer in the function just so I can return and assign it to the main() pointer-pointer.
I've got this defficiency for not settling with what "just works". I don't want you doing everything for me but I want good code and am open to criticism.

int** PntrFunc(int** itemGiven)
{
    int* newInt = new int;
    *newInt = 500 + **itemGiven;
    int** retPntr = &newInt;
    return retPntr;
}

int main()
{

    int theVar = 50;
    int* pntr = &theVar;

    int** mPntr = &pntr;

    mPntr = PntrFunc(mPntr);

    return 0;
}

Though your code is not strictly c++, this cuts down on some unneeded code.

int** PntrFunc(int** itemGiven)
{
    **itemGiven += 500;
    return itemGiven;
}

int main()
{
    int * pntr = new int;
    *pntr = 50;

    int ** mPntr = PntrFunc(&pntr);

    printf("result %d\n", **mPntr);
    delete pntr;

    "press Enter to exit\n";
    getchar();

    return 0;
}

Okay guys thanks for taking the time to help out a novice and give me a better insight into use of pointers in C++.
I leave with a feeling of gratitude and the all to usual feeling of stupidity when looking at what I was doing wrong.

I won't waste anymore of your time. Thanks again and bye.

Problem: Solved
:)

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.