Making a *Pointer point to a &(*pointer)

I'm trying to make a pointer point towards the address of another pointer, the youtube guy XOAX said its possible on his 'pointers and references tutorial' link http://www.youtube.com/watch?v=LwAPKuJpvmA&feature=watch_response

why doesn't my(**pPtr = &iAnotherpPtr) work?

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
30
31
32
33
34
#include<iostream>

void main() 
{using namespace std;

int iaArray[] = {1,2,3,4,5};
int iAnotherInt = 20;
int*ipPtr = 0;
int iInt = 10;
int *iAnotherpPtr = &iAnotherInt;

ipPtr = &(iaArray[0]);
cout << "this is first number in the array:  " << *ipPtr << endl;

++ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is second number in the array (because the ++ adds from first array value in array[1] which makes/2/):  " << *ipPtr << endl;

--ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is first number in the array (because the -- subtracts from the second value in the array [2] which makes /1/):   " << *ipPtr << endl;

ipPtr = ipPtr + 3;/* this add 3 from the previous reference in the ipPtr array*/
cout << "this is the fourth number in the array (because adding 3 from the first array address points to the forth array adress [4]):   " << *ipPtr << endl;

ipPtr = &iAnotherInt;/*this changes the value ipPtr is pointing to*/
cout << endl <<endl <<endl <<endl <<endl <<endl <<endl << "Changing pointer value" <<endl << "ipPtr is now pointing to another value/20/: " << *ipPtr << endl;

*ipPtr = iInt;
cout << "this time ipPtr points to an interger iInt (which is 10):   " <<endl;

//int **ipPtr =  &iAnotherpPtr; /*a pointer now points to another pointer*/ 
//cout << "*ipPtr points to iAnotherpPtr (which has the value 20):   " << *ipPtr << endl;

system("PAUSE");
}
You have two variables with the same name. int *ipPtr and int **ipPtr. And you need two dereferencing stars to print out the value of the second ipPtr on line 31.
R0mai

Xoax said you can change the value of a pointer, I thought I was just assigning *ipPtr a new value which is the address of iAnotherpPtr.

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
30
31
32
33
34
#include<iostream>

void main() 
{using namespace std;

int iaArray[] = {1,2,3,4,5};
int iAnotherInt = 20;
int*ipPtr = 0;
int iInt = 10;
int *iAnotherpPtr = &iAnotherInt;

ipPtr = &(iaArray[0]);
cout << "this is first number in the array:  " << *ipPtr << endl;

++ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is second number in the array (because the ++ adds from first array value in array[1] which makes/2/):  " << *ipPtr << endl;

--ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is first number in the array (because the -- subtracts from the second value in the array [2] which makes /1/):   " << *ipPtr << endl;

ipPtr = ipPtr + 3;/* this add 3 from the previous reference in the ipPtr array*/
cout << "this is the fourth number in the array (because adding 3 from the first array address points to the forth array adress [4]):   " << *ipPtr << endl;

ipPtr = &iAnotherInt;/*this changes the value ipPtr is pointing to*/
cout << endl <<endl <<endl <<endl <<endl <<endl <<endl << "Changing pointer value" <<endl << "ipPtr is now pointing to another value/20/: " << *ipPtr << endl;

*ipPtr = iInt;
cout << "this time ipPtr points to an interger iInt (which is 10):   " <<endl;

**ipPtr =  &iAnotherpPtr; /*a pointer now points to another pointer*/ 
cout << "*ipPtr points to iAnotherpPtr (which has the value 20):   " << *ipPtr << endl;

system("PAUSE");
}


copy and paste this to see my problem
I think the expectation on line 28 (describing line 27) is incorrect. Also, what are you expecting on line 30?
i want *ipPtr to point to the reference &iAnotherpPtr which is another pointer. the youtube guy XOAX said its possible in his tutorial.
Because in your file ipPtr is an int*, a pointer to an int
You use it as if it were a pointer to an int* (an int**)
Especially this **ipPtr, ipPtr is int* not int**

If you want to point to a pointer that points to an int (int**), you cant use int*

1
2
3
4
5
int** ipPtrPtr =  &iAnotherpPtr;
//  ipPtrPtr = &iAnotherpPtr = int**
// *ipPtrPtr =  iAnotherPtr  = &iAnotherInt = int*
//**ipPtrPtr = *iAnotherPtr  =  iAnotherInt = 20 = int
cout << "ipPtr points to &iAnotherpPtr (which points to iAnother int which has the value 20):   " << **ipPtr << endl;


Pointers can be very hard to understand if youre new to them, especially if you mix them with arrays =P
Last edited on
Skilless

Anyway I tried to use your way and it didn't work on my compiler (I'm using VC++ 2010).

Anyway copy and paste my code, hopefully you may see the problem

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
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>

void main() 
{using namespace std;

int iaArray[] = {1,2,3,4,5};
int iAnotherInt = 20;
int*ipPtr = 0;
int iInt = 10;
int *iAnotherpPtr = &iAnotherInt;

ipPtr = &(iaArray[0]);
cout << "this is first number in the array:  " << *ipPtr << endl;

++ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is second number in the array (because the ++ adds from first array value in array[1] which makes/2/):  " << *ipPtr << endl;

--ipPtr;/* this tells the *ipPtr array to access the next number in the array*/
cout << "this is first number in the array (because the -- subtracts from the second value in the array [2] which makes /1/):   " << *ipPtr << endl;

ipPtr = ipPtr + 3;/* this add 3 from the previous reference in the ipPtr array*/
cout << "this is the fourth number in the array (because adding 3 from the first array address points to the forth array adress [4]):   " << *ipPtr << endl;

ipPtr = &iAnotherInt;/*this changes the value ipPtr is pointing to*/
cout << endl <<endl <<endl <<endl <<endl <<endl <<endl << "Changing pointer value" <<endl << "ipPtr is now pointing to another value/20/: " << *ipPtr << endl;

*ipPtr = iInt;
cout << "this time ipPtr points to an interger iInt (which is 10):   " <<*ipPtr <<endl;

/**ipPtr =  &iAnotherpPtr; /*a pointer now points to another pointer*/ 
/*cout << "*ipPtr points to iAnotherpPtr (which has the value 20):   " << *ipPtr << endl;*/

//BUY SKILLESS programer c++.com
int** ipPtrPtr =  &iAnotherpPtr;
//  ipPtrPtr = &iAnotherpPtr = int**
// *ipPtrPtr =  iAnotherPtr  = &iAnotherInt = int*
//**ipPtrPtr = *iAnotherPtr  =  iAnotherInt = 20 = int
cout << "ipPtr points to &iAnotherpPtr (which points to iAnother int which has the value 20):   " << **ipPtr << endl;

system("PAUSE");
}
Id love to compile it but im at work right now so, sorry, i cant compile here =P

Also,
1
2
*ipPtr = iInt;
cout << "this time ipPtr points to an interger iInt (which is 10):   " <<*ipPtr <<endl;

Youre saying it points to another int, but youre actually changing the value of iAnotherInt to 10
*ipPtr = iInt; is the same as doing iAnotherInt = iInt

This also means that you shouldnt expect 20 at the end, but 10, because iAnotherInt has been set to 10!
Last edited on
Surprisingly this works perfectly

1
2
*ipPtr = iInt;
cout << "this time ipPtr points to an interger iInt (which is 10):   " <<*ipPtr <<endl;


the only issue is the

*pointer pointing *anotherpointer

Anyway thanks for the help
Compiled, and this line
 
cout << "ipPtr points to &iAnotherpPtr (which points to iAnother int which has the value 20):   " << **ipPtr << endl;

Change **ipPtr to **ipPtrPtr
(my bad lolz, i cant brain at work)
Last edited on
Topic archived. No new replies allowed.