`void` — C++ Keyword

`void` — C++ Keyword

The void keyword in C++: represents the absence of a type or value.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

void

Represents the absence of type. Used as a function return type when the function returns nothing, and as void* for untyped pointers.

Syntax

void function-name(params);      // returns nothing
void* ptr;                       // untyped pointer (no dereference)
(void)expression;                // discard expression result

Example

#include <print>
#include <cstdlib>

void greet(const char* name) {
    std::println("Hello, {}!", name);
    // returns void – no return value
}

// Generic memory from malloc is void*
void demo_void_ptr() {
    void* raw = std::malloc(64);
    if (!raw) return;

    // Must cast before use
    int* arr = static_cast<int*>(raw);
    arr[0] = 42;
    std::println("{}", arr[0]);
    std::free(raw);
}

int main() {
    greet("World");
    demo_void_ptr();

    // Discard [[nodiscard]] return value intentionally
    // (void)some_nodiscard_func();
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}