pointer to a pointer c++

Hello everyone ...... could you explain to me the difference between the 2 following codes:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
#include <iostream>

int main()
{
	//example number 1 
	int ival = 10; 
	int *p = &ival; 

	int dval = 15; 
	int *p2 = &dval;

	p2 = p; //what happens when i assign p to p2 ? 


}


I believe that in the first case, if we assign the value of p to p2, p2 and p 2 pointers become equal and then we can access the object pointed to by p (ival) through both pointers ......


but i don't know what happens in this kind of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


#include <iostream>

int main()
{
	//example number 2 
	int ival = 1024; 
	int *p = &ival;
	int **ppi = &p; 




}


in this code p is a pointer to an int and ppi is a pointer to a pointer to an int...... what is the difference between assigning a pointer to another and make a pointer point to another pointer? with both codes can access an object indirectly via two pointers?

In the first example it is like you said
if we assign the value of p to p2, p2 and p 2 pointers become equal and then we can access the object pointed to by p (ival) through both pointers

like if we write *p2=5; ival is changing

in the second it is a pointer to pointer
you can change p via ppi: *ppi=NULL; for example
you can also change ival: **ppi=6; or ++**ppi;

Edit: **ppi can be used as a dynamic bidimensional array
Last edited on
Topic archived. No new replies allowed.