⬅ Previous Next ➡

Pointers

Pointers in C
  • Pointer is a variable that stores the address of another variable.
  • Pointer helps in call by reference, dynamic memory allocation, arrays/strings handling, and efficient functions.
  • Main operators: & (address-of) and * (dereference/value-at).

1) Pointer Fundamentals

  • Declaration: data_type *ptr;
  • &var gives address of var, *ptr gives value stored at that address.
#include <stdio.h>

int main() {
    int a = 10;
    int *p = &a;

    printf("a = %d\n", a);
    printf("&a = %p\n", (void*)&a);
    printf("p = %p\n", (void*)p);
    printf("*p = %d\n", *p);

    return 0;
}

2) Pointer Arithmetic

  • Allowed operations: p++, p--, p + n, p - n, p1 - p2 (same array).
  • Pointer moves by sizeof(type) bytes automatically.
#include <stdio.h>

int main() {
    int a[3] = {10, 20, 30};
    int *p = a; // points to a[0]

    printf("*p = %d\n", *p);   // 10
    p++;
    printf("*p = %d\n", *p);   // 20
    p++;
    printf("*p = %d\n", *p);   // 30

    return 0;
}

3) Pointers with Arrays

  • Array name acts like pointer to first element: a == &a[0]
  • a[i] is same as *(a + i)
#include <stdio.h>

int main() {
    int a[5] = {2, 4, 6, 8, 10};

    for (int i = 0; i < 5; i++) {
        printf("a[%d]=%d  *(a+%d)=%d\n", i, a[i], i, *(a + i));
    }

    return 0;
}

4) Pointers with Strings

  • String is a char array ending with \0.
  • char *p can point to a string literal (read-only in many systems).
  • Use char str[] if you need to modify the string.
#include <stdio.h>

int main() {
    char str[] = "Hello";     // modifiable
    char *p = str;

    while (*p != '\0') {
        printf("%c", *p);
        p++;
    }
    printf("\n");

    return 0;
}

5) Pointers with Functions (Call by Reference)

  • Passing address allows function to modify original value.
#include <stdio.h>

void update(int *x) {
    *x = *x + 10;
}

int main() {
    int a = 5;
    update(&a);
    printf("a = %d\n", a); // 15
    return 0;
}

6) Function Pointer (Basic)

  • A function pointer stores address of a function.
  • Used in callbacks and menu-driven programs.
#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    int (*fp)(int, int) = add; // function pointer

    printf("Sum = %d\n", fp(10, 20));
    return 0;
}

7) Special Pointers

7.1) void Pointer
  • void* can store address of any type.
  • Must be typecast before dereferencing.
#include <stdio.h>

int main() {
    int a = 10;
    float b = 2.5f;

    void *p;

    p = &a;
    printf("a = %d\n", *(int*)p);

    p = &b;
    printf("b = %.2f\n", *(float*)p);

    return 0;
}
7.2) Wild Pointer
  • Wild pointer is an uninitialized pointer (contains garbage address).
  • Fix: always initialize pointer with NULL or valid address.
#include <stdio.h>

int main() {
    int *p = NULL; // safe initialization

    if (p == NULL) {
        printf("Pointer is NULL (safe)\n");
    }
    return 0;
}
7.3) Dangling Pointer
  • Dangling pointer points to freed memory or out-of-scope variable.
  • Fix: set pointer to NULL after free, avoid returning address of local variables.
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = (int*)malloc(sizeof(int));
    if (p == NULL) return 0;

    *p = 99;
    printf("%d\n", *p);

    free(p);   // memory released
    p = NULL;  // prevent dangling pointer

    return 0;
}

8) Quick Notes

  • NULL pointer points to nothing (safe default).
  • Pointer arithmetic works by size of data type.
  • Use pointers for arrays, strings, and call-by-reference.
  • Avoid wild/dangling pointers to prevent crashes.
⬅ Previous Next ➡