0
0
Cprogramming~5 mins

Pointers to pointers in C

Choose your learning style9 modes available
Introduction

Pointers to pointers let you store the address of a pointer. This helps when you want to change a pointer inside a function or manage multiple levels of data.

When you want a function to change the address stored in a pointer.
When working with arrays of strings (like list of words).
When managing dynamic multi-dimensional arrays.
When you need to pass a pointer by reference to another function.
Syntax
C
type **pointer_to_pointer;

Each * adds one level of indirection (address pointing).

Use ** to declare a pointer that points to another pointer.

Examples
Declares a pointer to a pointer to an int.
C
int **pptr;
Declares a pointer to a pointer to a char (often used for strings).
C
char **str_ptr;
Pointer pptr stores the address of pointer ptr.
C
int *ptr;
int **pptr = &ptr;
Sample Program

This program shows how a pointer to a pointer can access and change the original value.

C
#include <stdio.h>

int main() {
    int value = 10;
    int *ptr = &value;       // pointer to int
    int **pptr = &ptr;       // pointer to pointer to int

    printf("Value: %d\n", value);
    printf("Value via ptr: %d\n", *ptr);
    printf("Value via pptr: %d\n", **pptr);

    // Change value using pointer to pointer
    **pptr = 20;
    printf("New value after change via pptr: %d\n", value);

    return 0;
}
OutputSuccess
Important Notes

Remember to use ** to access the value when using a pointer to a pointer.

Pointer to pointer is useful for modifying pointers inside functions.

Summary

Pointer to pointer stores the address of another pointer.

Use ** to declare and access pointer to pointer values.

It helps when you want to change pointers or work with multi-level data.