ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

C Programming Tutorial• 8 min read

Pointers & Memory Addresses

The & address-of operator, * dereferencing, and pointer arithmetic

Pointers are variables that store the memory address of another variable. Pointers enable pass-by-reference simulation, dynamic memory, and raw hardware access.

Pointer Syntax & Operators

& (Address-Of Operator): Retrieves the memory address where a variable is stored (e.g. &x).

* (Dereference Operator): Accesses the actual value stored at the memory address pointed to by the pointer.

Pointer Arithmetic: Adding 1 to an int* pointer advances the memory address by sizeof(int) (4 bytes), pointing to the next adjacent integer.

Swapping Two Numbers via Pointers
#include <stdio.h>

// Pass-by-reference simulation using pointers
void swap(int *a, int *b) {
    int temp = *a; // Read value at address a
    *a = *b;       // Write value at address b into address a
    *b = temp;     // Write temp into address b
}

int main(void) {
    int x = 10, y = 20;
    printf("Before swap: x = %d, y = %d\n", x, y);

    swap(&x, &y); // Pass memory addresses
    printf("After swap:  x = %d, y = %d\n", x, y);
    return 0;
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

If `ptr` is an `int*` pointing to address 1000 on a 32-bit architecture, what address does `ptr + 1` point to?