Skip to content

Instantly share code, notes, and snippets.

@joegasewicz
Created December 6, 2022 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joegasewicz/f49e987abcd05aefe7c77be6f46069fa to your computer and use it in GitHub Desktop.
Save joegasewicz/f49e987abcd05aefe7c77be6f46069fa to your computer and use it in GitHub Desktop.
Double Pointers (pointer to a pointer)
#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