C void Pointers

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is void Pointers?

A void * is a generic pointer that can hold the address of any object. It cannot be dereferenced or used in arithmetic until it is cast to a concrete type. Functions like malloc, memcpy, and qsort use void * to work with any type.

Remember
void *vp = &x;          /* any object pointer converts to void * */
int  *ip = vp;          /* converts back (implicitly in C)       */
*(int *)vp = 5;         /* cast before dereferencing             */

Cheat Sheet

Keep these facts handy while you work with void Pointers in C.

TopicDetail
ConversionTo and from any object pointer without a cast in C
DereferenceNot allowed
ArithmeticNot standard; cast to unsigned char *
UseGeneric APIs

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: NULL Pointers.

Example 1 — A generic swap

Swap any two objects of equal size, byte by byte.

C
#include <stdio.h>

static void swap_any(void *a, void *b, size_t n) {
    unsigned char *x = a, *y = b;
    while (n--) {
        unsigned char t = *x;
        *x++ = *y;
        *y++ = t;
    }
}

int main(void) {
    int i = 1, j = 2;
    double d = 1.5, e = 2.5;

    swap_any(&i, &j, sizeof i);
    swap_any(&d, &e, sizeof d);
    printf("i=%d j=%d\n", i, j);
    printf("d=%.1f e=%.1f\n", d, e);
    return 0;
}

How It Works

  1. void * parameters accept the address of any type.
  2. Casting to unsigned char * allows byte-wise access.
  3. The caller supplies the object size.

Edge Cases & Pitfalls

Check these before relying on void Pointers in real programs.

deref

Dereferencing void *

*vp does not compile. Cast first: *(int *)vp.

type

Casting to the wrong type

The compiler cannot check the cast. Casting to the wrong type is undefined behavior.

Key Takeaways

  • Generic: holds any object address.
  • Cast: required to dereference.
  • Size: pass it separately.

One line: A void * is a generic pointer that can hold the address of any object.

Frequently Asked Questions

A void pointer provides generic access to memory without committing to a type, used in malloc, memcpy, qsort, and callback context arguments.
Standard C forbids it. GCC allows it as an extension, but portable code should cast to unsigned char * first.
Continue with "C NULL Pointers" next. Use NULL to mean "points to nothing" and check it before dereferencing. The C Pointers hub lists every topic in order.

Next: NULL Pointers

Use NULL to mean "points to nothing" and check it before dereferencing.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful