Pointer to pointer

Basic types / Pointers

//a function changes the caller's pointer through a pointer to it
void makeCopy(char **dest, const char *src) {
    *dest = malloc(strlen(src) + 1);
    if (*dest != nullptr) {
        strcpy(*dest, src);
    }
}

int a = 5;
int b = 9;
int *p = &a;
int **pp = &p;

//two stars reach the value
**pp = 7;
//a is 7

//one star reaches the pointer: now p points to b
*pp = &b;
//*p is 9

char *copy = nullptr;
makeCopy(&copy, "text");
//copy is "text"

//an array of strings is used through a pointer to pointers
const char *words[] = {"one", "two", "three"};
const char **pWords = words;
const char *second = pWords[1];
//second is "two"

printf("a is %d\n", a);
printf("*p is %d\n", *p);
printf("copy is %s\n", copy);
printf("second is %s\n", second);
free(copy);