`void` — C++ Keyword
`void` — C++ Keyword
The void keyword in C++: represents the absence of a type or value.
`void` — C++ Keyword
The void keyword in C++: represents the absence of a type or value.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
voidRepresents the absence of type. Used as a function return type when the function returns nothing, and as void* for untyped pointers.
void function-name(params); // returns nothing
void* ptr; // untyped pointer (no dereference)
(void)expression; // discard expression result
#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();
}
void* can hold any object pointer but cannot be dereferenced without a cast.void may use bare return; to exit early.void is not a complete type; you cannot declare a variable of type void.voidint 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;
}