Andrei Pall

Linux Software Engineering

How to use double pointers in C

A pointer is a variable that stores the memory address of another variable as its value.

#include <stdio.h>
#include <malloc.h>

void change_nr(int ** nr) {
    *nr = (int *) malloc(sizeof(int));
}

int main()
{
    int * p_nr = NULL;
    change_nr(&p_nr);
    * p_nr = 4;
    printf("%d", *p_nr);
    free(p_nr);
    return 0;
}