Coding

void in C: Functions, Parameters and void Pointers

void in C is a type with no values, and it shows up in three places: a function that returns nothing (void f(int x)), a function that takes no parameters (int f(void)), and a pointer to data of unknown type (void *). The meaning depends on where it appears, so each use is covered below.

#include <stdio.h>

void print_greeting(void)      /* returns nothing, takes nothing */
{
    printf("hello\n");
}

int main(void)
{
    int number = 7;
    void *anything = &number;   /* pointer to unknown type */
    int *back = anything;       /* convert back before use */

    print_greeting();
    printf("%d\n", *back);
    return 0;
}

Example output:

hello
7

void as a function return type

A function declared with a void return type performs an action and gives no value back. You cannot assign its result to a variable, and you cannot use it inside an expression that needs a value. A bare return; ends the function early. Reaching the closing brace has the same effect.

void swap_ints(int *a, int *b)
{
    int held = *a;
    *a = *b;
    *b = held;
}

Because a void function cannot hand back a result, it communicates through pointer parameters, global state or side effects such as printing and writing files. The swap_ints function above is the standard case: the caller passes addresses, and the function changes the values at those addresses. If you need to report success or failure, return an int status code instead of void.

void in a parameter list: f(void) versus f()

int f(void) declares a function that takes no parameters. int f() is not the same thing before C23. According to cppreference, f() declares a function with an unspecified number of parameters, so f(1) compiles and the call is undefined behavior if the definition does not match. With f(void), the call f(1) is a compile-time error. The same page notes that since C23 the empty list f() is equivalent to f(void).

DeclarationMeaning before C23Meaning in C23Call f(1)
int f(void);No parametersNo parametersCompile-time error
int f();Unspecified parametersNo parametersUndefined behavior before C23, error in C23

The safe habit that works under every standard version is to write (void) whenever a function takes nothing, including int main(void). That gives you argument checking from the compiler and does not depend on which C standard your build uses. Check your compiler's standard flag if you are unsure, because many projects still compile as C11 or C17.

What a void pointer is

A void * is a pointer to an object of unknown type. cppreference states that a pointer to an object of any type can be implicitly converted to a pointer to void, and the other way round. The page's own example shows both directions:

int n = 1, *p = &n;
void *pv = p;     /* int * to void * */
int *p2 = pv;     /* void * to int *  */
printf("%d\n", *p2);

This is what makes generic interfaces possible in C. A function that takes void * can accept the address of an int, a struct or a buffer of bytes, and it does not need a separate version for each. The cppreference page lists malloc, qsort callbacks and pthread_create callbacks as examples, and says plainly that "it is the caller's responsibility to convert the pointer to the correct type before use". If you convert to the wrong type, the compiler cannot warn you, so the mistake appears at run time.

For the basics of what a pointer is and how addresses and dereferencing work, see Pointers in C Explained With Examples.

Why you cannot dereference a void pointer

The expression *pv is invalid when pv is a void *. The cppreference type page lists void as an incomplete type that cannot be completed, so there is no object type to read through the pointer. The compiler does not know how many bytes to load or how to interpret them. Convert first:

void *pv = &number;
int value = *(int *)pv;     /* cast, then dereference */
/* or */
int *typed = pv;
int value2 = *typed;

Pointer arithmetic has the same limit. Arithmetic on a pointer moves by the size of the type it points to, and a void pointer has no such size. The cppreference pointer page describes addition and subtraction as operations on pointers to array elements and does not define them for void *. Some compilers accept it as an extension, which can hide the problem in code that later moves to another compiler. If you need to step through raw bytes, convert to unsigned char * first, so the step size is one byte by definition.

Example: void pointers in qsort

The standard library's sorting function is the clearest everyday use. Its prototype, from cppreference, is:

void qsort(void *ptr, size_t count, size_t size,
           int (*comp)(const void *, const void *));

qsort receives the array as void * plus the element size, so it can move bytes around without knowing the element type. Your comparison function receives two const void * pointers and must cast them back:

#include <stdio.h>
#include <stdlib.h>

int compare_ints(const void *a, const void *b)
{
    int left = *(const int *)a;
    int right = *(const int *)b;
    return (left > right) - (left < right);
}

int main(void)
{
    int values[] = {42, -7, 19, 0};
    size_t count = sizeof values / sizeof *values;

    qsort(values, count, sizeof *values, compare_ints);

    for (size_t i = 0; i < count; i++)
        printf("%d ", values[i]);
    printf("\n");
    return 0;
}

Example output:

-7 0 19 42

The cppreference page warns against the shortcut return left - right;, because the subtraction can overflow, which is undefined behavior for values such as INT_MIN. The comparison form above avoids that. The page also says qsort does not guarantee stability, so equal elements can end up in any order.

Example: void pointers in malloc

malloc returns void * because it does not know what you will store in the block. In C the result converts implicitly, so int *items = malloc(count * sizeof *items); needs no cast. Check the result for NULL before using it, and release the block with free. The full rules are in malloc in C: Allocate, Free and Avoid Memory Leaks. Copying raw bytes with void * parameters works the same way, as described in memcpy in C: Syntax, memmove Difference and Safe Use.

Writing your own function that takes void *

A generic function needs two pieces of information besides the pointer: how big each element is and how many there are. Without the size, the function cannot step through the data. This small helper fills a buffer of any element type with one repeated element, using unsigned char * for byte-level addressing:

#include <string.h>

void fill_array(void *array, size_t count, size_t element_size,
                const void *element)
{
    unsigned char *cursor = array;
    for (size_t i = 0; i < count; i++) {
        memcpy(cursor, element, element_size);
        cursor += element_size;
    }
}

Call it as fill_array(values, 4, sizeof values[0], &seven);. The compiler will accept a wrong element_size, and the function will then write past the end of the array, so a generic interface moves the type-checking burden onto you. Use const void * for inputs the function only reads, as qsort does for its comparator arguments.

Common mistakes with void in C

  • Using void as a variable type. void x; is an error because void is an incomplete type. Only void * variables are allowed.
  • Writing main() in code that should be strict. Prefer int main(void) so the compiler knows there are no parameters.
  • Returning a value from a void function. return 5; inside a void function is a constraint violation, and compilers report it.
  • Casting a void pointer to the wrong type. The compiler accepts *(double *)pv even if pv points to an int. Keep the type next to the pointer in the code, for example in a struct field, so you do not lose track of it.
  • Converting function pointers to void *. The cppreference conversion rule covers pointers to objects only, and function pointers follow separate rules, so do not assume the standard allows it.

Frequently asked questions

What does void mean in C?
void is a type with no values. As a function return type it means the function returns nothing, in a parameter list written as (void) it means the function takes no arguments, and in void * it means a pointer to an object of unknown type. The cppreference type page lists void as an incomplete type that cannot be completed.
What is the difference between f() and f(void) in C?
In C before C23, f() declares a function with an unspecified parameter list, so calling it with arguments compiles and is undefined behavior if they do not match the definition. f(void) declares a function with no parameters, and a call with arguments is a compile-time error. Since C23, f() is equivalent to f(void).
Can you dereference a void pointer in C?
No. void is not a complete object type, so *pv is invalid. Convert the void pointer to a pointer of the real type first, for example int *p = pv; and then use *p. The caller is responsible for converting to the correct type.
Do you need to cast the result of malloc in C?
No. In C a void * converts implicitly to any object pointer type, so int *p = malloc(n * sizeof *p); is correct without a cast. C++ does require the cast.

Sources