passing reference of pointer vs pointer of pointer

this didn't go well the first time so im giving it another shoot with a code snippet to make it easier as my english is poor enough to ask about this

edit: ill make that as short as possible instead of 50 lines of blocktextblocktext

at this code snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
 
int nFive = 5;
int nSix = 6;
 
void SetToSix(int *pTempPtr);
 
int main()
{
    using namespace std;
 
    int *pPtr = &nFive;
 
    cout << *pPtr;
 
    SetToSix(pPtr);
 
    cout << *pPtr;
 
    return 0;
}
 
void SetToSix(int *pTempPtr)
{
    using namespace std;
    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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

    using namespace std;

int nFive = 5;
int nSix = 6;

void SetToSix(int *pTempPtr);
 
int main()
{
    int *pPtr = &nFive;
    int *pPtr2 = &pPtr

    SetToSix(pPtr2);
 
    cout << *pPtr;
 
    return 0;
}
 

void SetToSix(int *pTempPtr)
{
    *pTempPtr = &nSix;
}


would that do the trick and make pPtr points to nSix ?
thanks and sorry for the misunderstanding in the last thread
Last edited on
Lines 13 and 25 in your second code snippet are not entirely correct.

Line 13:
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.

Line 25:
The indirection operator (*) dereferences the pointer, so it is not a pointer anymore and is the object itself. Line 25 could be translated to:
1
2
//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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

typedef int* pint_t; 

int nFive = 5;
int nSix = 6;
 
void SetToSix(pint_t& pTempPtr)
{
    pTempPtr = &nSix;
}
 
int main()
{
    int *pPtr = &nFive;
    std::cout << *pPtr << std::endl;
 
    SetToSix(pPtr);
    std::cout << *pPtr << std::endl;
 
    return 0;
}
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 ?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int var;
int var2;
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;
(too late)
Last edited on
Topic archived. No new replies allowed.