Module 8 of 13 · C Programming Fundamentals · Beginner

Pointers

Duration: 60 min

Pointers are variables that store memory addresses. They are one of the most powerful features of C, enabling dynamic memory allocation, passing variables by reference, and creating complex data structures. Understanding pointers is essential for mastering C.

Pointer Declaration & Initialization

#include <stdio.h>

int main() {
    int x = 10;
    int *ptr;  // Declare a pointer to int
    
    ptr = &x;  // ptr now holds the address of x
    
    printf("Value of x: %d\n", x);
    printf("Address of x: %p\n", &x);
    printf("Value of ptr: %p\n", ptr);
    
    return 0;
}

Dereferencing Pointers

#include <stdio.h>

int main() {
    int x = 10;
    int *ptr = &x;
    
    // Dereference: access the value at the address
    printf("Value at address: %d\n", *ptr);  // 10
    
    // Modify through pointer
    *ptr = 20;
    printf("x is now: %d\n", x);  // 20
    
    return 0;
}

Pointer Arithmetic

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr = arr;  // ptr points to first element
    
    printf("First element: %d\n", *ptr);      // 10
    printf("Second element: %d\n", *(ptr + 1)); // 20
    printf("Third element: %d\n", *(ptr + 2));  // 30
    
    // Increment pointer
    ptr++;
    printf("After ptr++: %d\n", *ptr);  // 20
    
    // Pointer difference
    int *ptr1 = &arr[0];
    int *ptr2 = &arr[3];
    printf("Difference: %ld\n", ptr2 - ptr1);  // 3
    
    return 0;
}

Pointers to Pointers

#include <stdio.h>

int main() {
    int x = 10;
    int *ptr = &x;
    int **pptr = &ptr;  // Pointer to pointer
    
    printf("x: %d\n", x);
    printf("*ptr: %d\n", *ptr);
    printf("**pptr: %d\n", **pptr);
    
    // Modify through pointer to pointer
    **pptr = 20;
    printf("x is now: %d\n", x);  // 20
    
    return 0;
}

NULL Pointer

#include <stdio.h>

int main() {
    int *ptr = NULL;  // NULL pointer (doesn't point to anything)
    
    // Always check before dereferencing
    if (ptr != NULL) {
        printf("Value: %d\n", *ptr);
    } else {
        printf("Pointer is NULL\n");
    }
    
    // Dereferencing NULL causes crash
    // printf("%d\n", *ptr);  // CRASH!
    
    return 0;
}

Pointers and Arrays

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr = arr;  // Array name is a pointer to first element
    
    // Access array elements using pointer
    for (int i = 0; i < 5; i++) {
        printf("%d ", *(ptr + i));
    }
    printf("\n");
    
    // Equivalent to array indexing
    printf("arr[2] = %d\n", arr[2]);
    printf("*(ptr + 2) = %d\n", *(ptr + 2));
    
    return 0;
}

Pointers and Functions

#include <stdio.h>

// Pass by reference using pointers
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

// Function returning a pointer
int* getMax(int *a, int *b) {
    return (*a > *b) ? a : b;
}

int main() {
    int x = 5, y = 10;
    
    printf("Before swap: x=%d, y=%d\n", x, y);
    swap(&x, &y);
    printf("After swap: x=%d, y=%d\n", x, y);
    
    int *max = getMax(&x, &y);
    printf("Max: %d\n", *max);
    
    return 0;
}

Void Pointers

#include <stdio.h>

int main() {
    int x = 10;
    float y = 3.14;
    
    void *ptr;  // Generic pointer
    
    ptr = &x;
    printf("Integer: %d\n", *(int*)ptr);  // Cast to int*
    
    ptr = &y;
    printf("Float: %.2f\n", *(float*)ptr);  // Cast to float*
    
    return 0;
}

Quiz 1: Address-of Operator

❓ What does the & operator do?

Quiz 2: Dereferencing

❓ What does the * operator do when used with a pointer?

Quiz 3: Pointer Arithmetic

❓ If ptr points to an int and you do ptr++, how many bytes does it advance?

Quiz 4: NULL Pointer

❓ What is a NULL pointer?

Quiz 5: Pointer to Pointer

❓ What does ** do with a pointer to pointer?

← Previous Continue interactively → Next →

Related Courses