Skip to content
C

Pointer Basics

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

By EZ4Code Team
pointermemory

Code

#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;
}

Explanation

A pointer stores the address of another variable, and the dereference operator * reads or writes the value at that address. Arrays decay to pointers when used in expressions, so pointer arithmetic can iterate over elements. Passing pointers to functions lets them mutate caller-owned data without copying it.

More C Snippets