int nFive = 5;
int nSix = 6;
void SetToSix(int *pTempPtr);
int *pPtr = &nFive;
cout << *pPtr;
cout << *pPtr;
void SetToSix(int *pTempPtr)
pTempPtr = some value;
cout << *pTempPtr;
we are actually passing a value (which is address of nFive) through the pointer, the function takes copy of this address and declare a new pointer called pTempPtr which points to the same memory and then we can assign values to this memory which will indeed change the value of the very first variable pointing to it (the nFive)
but we cannot change the address of the pointer we used as an argument aka pPtr but we can change the value of this pointer "pTempPtr"
so i was wondering if we can do something like the following
Lines 13 and 25 in your second code snippet are not entirely correct.
A pointer by itself just holds the memory address of what it is pointing to. So when you initialize pPtr2, you don't use the address-of operator (&) with another pointer. In fact, you are assigning the address of pPtr to pPtr2 and not the address of nFive on line 13. This should cause an error because the types are not compatible, i.e. a int* pointer expects to point to an int and not an int*, which is what int** pointers point to.
The indirection operator (*) dereferences the pointer, so it is not a pointer anymore and is the object itself. Line 25 could be translated to:
//Assuming you fixed line 13 to copy the pointer
nFive = &nSix;
Unfortunately, this line may compile without error because nFive will now hold some number that is equal to the memory address value of nSix.
Just drop the asterisk, so you are assigning a new memory address to a pointer.
im sorry but this is pretty confusing to me (didn't even compile it)
anyway assume the following
nFive value is 5 and address is 0x555
pPtr value is 0x555 and address is 0x666
i wanted to assign the address of pPtr to pPtr2 to be
pPtr2 value is 0x666 and address of 0x777 (assuming the numbers so i can explain using them)
what i want to do in that function is to change the value of pPtr without using reference to that pointer with using a pointer to that pointer
as when i pass the pointer to this function it will get a copy of the address of 0x666
and when it assign that address to the pointer in paramater "pTempPtr" then ill be able to change the value of pTempPtr (which should currently be 0x555) to whatever i want
i wish that make sense because im almost dying from not being able to explain what i want to do due to being new in c++ and having a poor english :\
@kbw isn't that the same concept of passing pointer by reference ?
int* intptr = &var; //points to an int
int* secptr = &var2;
int** intptrptr = &intptr; //points to an int* that points to an int
//Assigning new memory address
*intptrptr = secptr;
//which is the same as
intptr = secptr;