Skip to content
C

Pointer Basics

Declare pointers, dereference, and walk an array with pointer arithmetic.

#pointer#memory

Code

c
#include <stdio.h>

int main(void) {
    int x = 42;
    int *p = &x;            // pointer holds address of x

    printf("x      = %d\n", x);
    printf("&x     = %p\n", (void*)&x);
    printf("p      = %p\n", (void*)p);
    printf("*p     = %d\n", *p);   // dereference

    *p = 100;               // modify x through pointer
    printf("x now  = %d\n", x);

    int arr[3] = {10, 20, 30};
    int *q = arr;            // array decays to pointer
    for (int i = 0; i < 3; i++) {
        printf("arr[%d] = %d\n", i, *(q + i));
    }
    return 0;
}