Created
December 6, 2022 08:59
-
-
Save joegasewicz/f49e987abcd05aefe7c77be6f46069fa to your computer and use it in GitHub Desktop.
Double Pointers (pointer to a pointer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
/* ====================================================================== */ | |
/* Double Pointers (Pointer to a Pointer) */ | |
/* ====================================================================== */ | |
// 2 levels of indirection | |
int **dblPtr; | |
int a = 1; | |
int b = 2; | |
int c = 3; | |
int *ptrOne = &a; | |
int *ptrTwo = &b; | |
// assign the double pointer | |
dblPtr = &ptrOne; // if you try to use ptrOne it will only be pointing at the pointers address | |
printf("dblPtr = %d\n", **dblPtr); // 1 | |
printf("ptrOne = %d\n", ptrOne); // the address of &a | |
printf("&ptrOne = %u\n", &ptrOne); // the address of ptrOne | |
printf("dblPtr = %d\n", dblPtr); // the address of ptrOne | |
printf("ptrOne == &a %d\n", ptrOne == &a); // true (1) | |
printf("dblPtr == &ptrOne %d\n", dblPtr == &ptrOne); // true (1) | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment