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?