Skip to main content

Lesson 4 of 6

Pointer to Pointer - int** & the argv Pattern

A pointer to a pointer (`int**`) is a variable that stores the address of another pointer. The canonical real-world example is `char **argv` - an array of strings, where each string is a `char*`. Using `int**` allows a function to modify the caller's pointer variable (not just the value it points to), which is the pattern for functions like `malloc` wrappers that allocate AND set a pointer.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Pointer to Pointer - argv, String Arrays & Pointer Modification

Pointer to Pointer - argv, String Arrays & Pointer Modification
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ── Double pointer to modify caller's pointer ───────────── */
void allocate_string(char **out, const char *value) {
    /* *out = the caller's pointer variable.
       Without **, we'd get a copy and the caller's ptr unchanged. */
    *out = (char *)malloc(strlen(value) + 1);
    if (*out) strcpy(*out, value);
}

/* ── Processing argv - array of strings ─────────────────── */
void print_args(int argc, char **argv) {
    /* char **argv: pointer to first element of array of char*
       argv[0] = program name
       argv[1] = first argument
       argv[argc] = NULL (sentinel)                           */
    printf("argc = %d
", argc);
    for (int i = 0; i < argc; i++) {
        printf("argv[%d] = %s
", i, argv[i]);
    }
}

/* ── Simulating argv ─────────────────────────────────────── */
int main(void) {
    /* Create an array of string pointers */
    char *words[] = { "hello", "world", "foo", NULL };
    /* words is char*[] - decays to char**                    */

    printf("Double pointer traversal:
");
    for (char **w = words; *w != NULL; w++) {
        /* *w = current char* (string)
           **w = first character of current string             */
        printf("  word='%s'  first_char='%c'
", *w, **w);
    }

    /* ── Modify a pointer via double pointer ─────────────── */
    char *msg = NULL;
    allocate_string(&msg, "Hello from double pointer!");
    if (msg) {
        printf("%s
", msg);
        free(msg);
    }

    /* ── char** vs char*[] - interchangeable in function params */
    /* Both are the same when passed to a function.
       But in declarations:
       char *arr[] = {...}  - array of pointers (stack)
       char **p             - a pointer to a pointer (can point anywhere) */

    /* ── Memory diagram ──────────────────────────────────────
       words array (stack):
       ┌─────────┐     ┌───────┐
       │ words[0]│────>│"hello"│ (string literal in rodata)
       ├─────────┤     └───────┘
       │ words[1]│────>│"world"│
       ├─────────┤     └───────┘
       │ words[2]│────>│"foo"  │
       ├─────────┤     └───────┘
       │ words[3]│────>│ NULL  │
       └─────────┘
       w (pointer to pointer):
       ┌───┐     ┌─────────┐
       │ w │────>│words[0] │
       └───┘     └─────────┘                                   */

    return 0;
}

Common Mistakes

  • Passing a pointer where a double pointer is needed - `void alloc(char *p) { p = malloc(10); }` - the caller's pointer doesn't change. You need `void alloc(char **p) { *p = malloc(10); }`.
  • Confusing `*argv` and `argv[0]` - they are the same: both give the first `char*` in the argv array. `**argv` and `argv[0][0]` both give the first character of `argv[0]`.
  • Forgetting the NULL sentinel in argv - the standard guarantees `argv[argc] == NULL`. Use this for robust argument loops: `while (*argv) { printf("%s", *argv++); }`.

Tip

Tip

Practice Pointer to Pointer int the argv Pattern in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Double pointers allow functions to modify caller pointer addresses and navigate pointer arrays

Practice Task

Note

Practice Task - (1) Write a working example of Pointer to Pointer int the argv Pattern from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Pointer to Pointer int the argv Pattern is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • A pointer to a pointer (`int**`) is a variable that stores the address of another pointer.
  • Passing a pointer where a double pointer is needed - `void alloc(char *p) { p = malloc(10); }` - the caller's pointer doesn't change. You need `void alloc(char **p) { *p = malloc(10); }`.
  • Confusing `*argv` and `argv[0]` - they are the same: both give the first `char*` in the argv array. `**argv` and `argv[0][0]` both give the first character of `argv[0]`.
  • Forgetting the NULL sentinel in argv - the standard guarantees `argv[argc] == NULL`. Use this for robust argument loops: `while (*argv) { printf("%s", *argv++); }`.

Topics in This Module

0/6 done