Skip to content

Commit 5e2d749

Browse files
committed
Double pointers(pointer to pointer)
1 parent 9630d18 commit 5e2d749

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
int **double_pointer=NULL;
6+
7+
int var1=10;
8+
int var2=43;
9+
int var3=64;
10+
11+
int *pVar1=&var1;
12+
int *pVar2=&var2;
13+
int *pVar3=&var3;
14+
15+
double_pointer=&pVar1;
16+
17+
printf("The memory address where \"double_pointer\" variable is stored is %p.\n",&double_pointer);
18+
printf("The value stored in the \"double pointer\" variable %p which is the memory address it points to %p.\n",double_pointer,&pVar1);
19+
printf("The value to which the \"double pointer\" points to is %p.\n", *double_pointer );
20+
printf("The value derefenced of the \"double pointer\" variable is %d=%d.\n",**double_pointer,*pVar1);
21+
puts("--------------------------------");
22+
23+
*double_pointer=pVar2; //it means the double_pointer variable is dereferenced as pVar2, so to say the value it points to is pVar2. Hence since double_pointer=&pVar1 it means the pVar1 points to pVar2;
24+
25+
printf("The value stored in the \"double pointer\" variable %p which is the memory address it points to %p.\n",double_pointer,&pVar1);
26+
printf("The value to which the \"double pointer\" points to is %p which is the memory address of where \"pVar2\".\n", *double_pointer );
27+
printf("The value derefenced of the \"double pointer\" variable is %d=%d.\n",**double_pointer,*pVar2);
28+
29+
puts("--------------------- from the points of view of variable \"pVar1\"------------------");
30+
printf("The value stored in the variable \"pVar1\" is %p=%p.\n",pVar1,pVar2);
31+
printf("The value to which the variable \"pVar1\" points to is %d=%d.\n",*pVar1,*pVar2);
32+
33+
return 0;
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
int **pointer_to_pointer=NULL;
6+
7+
int var1=10;
8+
int var2=43;
9+
int var3=66;
10+
11+
int *pVar1=&var1;
12+
int *pVar2=&var2;
13+
int *pVar3=&var3;
14+
15+
pointer_to_pointer=&pVar1;
16+
17+
printf("The memory address where the \"pointer_to_pointer\" variable is stored is %p.\n",&pointer_to_pointer);
18+
printf("The value of the \"pointer_to_pointer\" variable which is the same value as the memory address where the variable \"pVar1\" is stored is %p=%p.\n",pointer_to_pointer,&pVar1);
19+
printf("The dereferenced \"pointer_to_pointer\" variable so to say the value that exists where \"pointer_to_pointer\" variable is pointing to is %p=%p.\n",*pointer_to_pointer,pVar1);
20+
printf("The completely derefenced value of the \"pointer_to_pointer\" variable which the value where the intermediate pointer variable points to is %d=%d.\n",**pointer_to_pointer,*pVar1);
21+
puts("=============================");
22+
23+
*pointer_to_pointer=&var3;
24+
printf("The memory address where the \"pointer_to_pointer\" variable is stored is %p.\n",&pointer_to_pointer); //these remain the same as above
25+
printf("The value of the \"pointer_to_pointer\" variable which is the same value as the memory address where the variable \"pVar1\" is stored is %p=%p.\n",pointer_to_pointer,&pVar1); //these remain the same as above
26+
printf("The dereferenced \"pointer_to_pointer\" variable so to say the value that exists where \"pointer_to_pointer\" variable is pointing to is %p=%p.\n",*pointer_to_pointer,pVar1); //due to the change that affects the pointer_to_pointer, the intermediat pointer variable is affected too
27+
printf("The completely derefenced value of the \"pointer_to_pointer\" variable which the value where the intermediate pointer variable points to is %d=%d.\n",**pointer_to_pointer,*pVar1);
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
int **double_pointer=NULL;
6+
7+
int var1=10,var2=43,var3=65;
8+
9+
int *pVar1=&var1;
10+
int *pVar2=&var2;
11+
int *pVar3=&var3;
12+
13+
double_pointer=&pVar1;
14+
15+
printf("The value of the double pointer is %p and this is the memory address it points to (=%p) \n", double_pointer,&pVar1);
16+
printf("The memory address where is stored the double pointer is %p. \n",&double_pointer);
17+
printf("The value stored at the memory address where the double pointer is pointing to is %p while the address the address stored in the intermediate pointer is %p .\n",*double_pointer,pVar1);
18+
printf("The final value to where the chain of pointer to pointer is pointing to is %d.\n",**double_pointer);
19+
20+
21+
return 0;
22+
}

Advanced Pointers/example.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
int a=10;
6+
7+
int *ptr=NULL;
8+
int **ptrptr=NULL;
9+
10+
ptr=&a;
11+
ptrptr=&ptr;
12+
13+
printf("The memory address where the variable \"a\" is stored is %p.\n",&a);
14+
printf("The memory address where the variable \"ptr\"i is stored is %p.\n",&ptr);
15+
printf("The memory address where the variabe \"ptrptr\" is stored is %p.\n",&ptrptr);
16+
17+
printf("The value stored at the memory address stored by \"ptr\" is %d.\n",*ptr); //should be a
18+
printf("The value stored at the memory address stored by \"ptrptr\" is %p.\n",*ptrptr); //should be &a
19+
printf("The value of **\"ptrptr\" is %d.\n ",**ptrptr);
20+
21+
return 0;
22+
}

0 commit comments

Comments
 (0)