Skip to main content

Lesson 5 of 6

void* - The Generic Pointer & const Correctness

`void*` is a pointer to memory of unknown type. It can receive any pointer without a cast and can be cast to any pointer type. It is the foundation of generic C APIs (`malloc`, `memcpy`, `qsort`, `bsearch`). **`const` correctness** with pointers - understanding the difference between `const T*`, `T* const`, and `const T* const` - is a mark of professional C code and enforces read-only guarantees in APIs.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

void* and const Pointer Types - All Four Variants

void* and const Pointer Types - All Four Variants
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ── void* - generic pointer ─────────────────────────────
   void* can hold any pointer.
   Must be cast before dereferencing (no size/type known).    */
void *allocate(size_t bytes) {
    return malloc(bytes);   /* malloc returns void* */
}

void copy_generic(void *dst, const void *src, size_t n) {
    /* Re-implement memcpy concept */
    unsigned char *d = (unsigned char *)dst;
    const unsigned char *s = (const unsigned char *)src;
    for (size_t i = 0; i < n; i++) d[i] = s[i];
}

/* ── const pointer variants ──────────────────────────────
   1. const T* (or T const*) - pointer to const T
      You cannot modify the VALUE pointed to.
      You CAN change where the pointer points.               */
void read_string(const char *s) {
    /* s[0] = 'X';  ← compile error: modifying const data   */
    printf("read: %s
", s);
    s++;   /* OK: advancing the pointer is allowed           */
}

/* ── API design with const: pass read-only data as const T* */
size_t my_strlen(const char *s) {
    /* Declares: "I will not modify your string"
       Callers can safely pass string literals or const data. */
    size_t len = 0;
    while (*s++) len++;
    return len;
}

int main(void) {
    /* ── void* usage ──────────────────────────────────────── */
    int *ip = (int *)allocate(sizeof(int) * 3);
    if (!ip) return 1;
    ip[0] = 10; ip[1] = 20; ip[2] = 30;

    int copy[3];
    copy_generic(copy, ip, sizeof(int) * 3);
    printf("copy: %d %d %d
", copy[0], copy[1], copy[2]);
    free(ip);

    /* ── const pointer variants - all four ───────────────── */

    int x = 42, y = 99;

    /* 1. const int* - pointer to const int
       CAN change ptr, CANNOT change the value               */
    const int *p1 = &x;
    p1 = &y;         /* OK: pointer can be redirected        */
    /* *p1 = 0;     ← compile error                         */

    /* 2. int* const - const pointer to int
       CANNOT change ptr, CAN change the value               */
    int * const p2 = &x;
    *p2 = 50;        /* OK: value can be modified            */
    /* p2 = &y;     ← compile error: pointer is const       */

    /* 3. const int* const - const pointer to const int
       CANNOT change ptr, CANNOT change value                */
    const int * const p3 = &x;
    /* *p3 = 0;     ← error: const value                    */
    /* p3 = &y;     ← error: const pointer                  */
    printf("p3 = %d
", *p3);  /* read-only access is fine  */

    /* 4. int* (no const) - mutable pointer to mutable int   */
    int *p4 = &x;
    *p4 = 77;   /* OK */
    p4 = &y;    /* OK */

    /* Memory trick to read const: "read right to left"
       const int *p  → p is a pointer to const int
       int * const p → p is a const pointer to int           */

    read_string("immutable string literal");
    printf("my_strlen = %zu
", my_strlen("hello"));

    return 0;
}

Common Mistakes

  • Discarding const via cast - `void f(const char *s) { char *w = (char *)s; w[0]='X'; }` - casting away const then writing to it is undefined behaviour if the original object was declared const. Never discard const.
  • Not making function parameters `const` for read-only input - `int strlen(char *s)` should be `int strlen(const char *s)`. Without const, callers with `const char*` get a warning and it prevents passing string literals.
  • Confusing `const int *` and `int * const` - the classic mix-up. Use the right-to-left reading rule: `const int *p` = 'p is a pointer to int that is const'. `int * const p` = 'p is a const pointer to int'.

Tip

Tip

Practice void The Generic Pointer const Correctness in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

void* enables generic data manipulation while const qualifiers enforce read-only semantics

Practice Task

Note

Practice Task - (1) Write a working example of void The Generic Pointer const Correctness from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with void The Generic Pointer const Correctness is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • `void*` is a pointer to memory of unknown type.
  • Discarding const via cast - `void f(const char *s) { char *w = (char *)s; w[0]='X'; }` - casting away const then writing to it is undefined behaviour if the original object was declared const. Never discard const.
  • Not making function parameters `const` for read-only input - `int strlen(char *s)` should be `int strlen(const char *s)`. Without const, callers with `const char*` get a warning and it prevents passing string literals.
  • Confusing `const int *` and `int * const` - the classic mix-up. Use the right-to-left reading rule: `const int *p` = 'p is a pointer to int that is const'. `int * const p` = 'p is a const pointer to int'.

Topics in This Module

0/6 done