A void* does not mean anything. It is a pointer, but the type that it points to is not known.

It's not that it can return "anything". A function that returns a void* generally is doing one of the following:

  • It is dealing in unformatted memory. This is what operator new and malloc return: a pointer to a block of memory of a certain size. Since the memory does not have a type (because it does not have a properly constructed object in it yet), it is typeless. IE: void.
  • It is an opaque handle; it references a created object without naming a specific type. Code that does this is generally poorly formed, since this is better done by forward declaring a struct/class and simply not providing a public definition for it. Because then, at least it has a real type.
  • It returns a pointer to storage that contains an object of a known type. However, that API is used to deal with objects of a wide variety of types, so the exact type that a particular call returns cannot be known at compile time. Therefore, there will be some documentation explaining when it stores which kinds of objects, and therefore which type you can safely cast it to.

This construct is nothing like dynamic or object in C#. Those tools actually know what the original type is; void* does not. This makes it far more dangerous than any of those, because it is very easy to get it wrong, and there's no way to ask if a particular usage is the right one.

And on a personal note, if you see code that uses void*'s "often", you should rethink what code you're looking at. void* usage, especially in C++, should be rare, used primary for dealing in raw memory.

Answer from Nicol Bolas on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › void-pointer-c-cpp
void Pointer in C - GeeksforGeeks
... // C program to demonstrate ... dereferenced with `*(int*)ptr` to get the value at // that memory location printf("%d", *(int*)ptr); return 0; }...
Published   July 17, 2014
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Void-Pointers.html
Void Pointers (GNU C Language Manual)
Passing an argument of type void * for a parameter that has a pointer type also converts. For example, supposing the function hack is declared to require type float * for its parameter, this call to hack will convert the argument to that type.
People also ask

Is there any difference between the null pointer and the void pointer in C?
The null pointer is basically used in a program to assign the value 0 to a pointer variable of any data type. The void pointer, on the other hand, has no value assigned to it and we use it to store the addresses of other variables in the program- irrespective of their data types.
🌐
byjus.com
byjus.com › gate › void-pointer-in-c
Void Pointer in C
What is the difference between a general pointer and a void pointer in C?
They are both the same. The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.
🌐
byjus.com
byjus.com › gate › void-pointer-in-c
Void Pointer in C
Why do we use a void pointer in C programs?
We use the void pointers to overcome the issue of assigning separate values to different data types in a program. The pointer to void can be used in generic functions in C because it is capable of pointing to any data type. One can assign the void pointer with any data type’s address, and then assign the void pointer to any pointer without even performing some sort of explicit typecasting. So, it reduces complications in a code.
🌐
byjus.com
byjus.com › gate › void-pointer-in-c
Void Pointer in C
🌐
Learn C++
learncpp.com › cpp-tutorial › void-pointers
19.5 — Void pointers – Learn C++
A void pointer must be explicitly cast into another type of pointer to perform indirection. A null pointer is a pointer that does not point to an address. A void pointer can be a null pointer. Thus, a void pointer refers to the type of the pointer, whereas a null pointer refers to the value (address) of the pointer.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_void_pointer.htm
void Pointer in C
You can also store pointers to any user-defined data types such as arrays and structures in a void pointer. In this example program, we have declared an array of void pointers and stored in it the pointers to variables of different types (int, float, and char *) in each of its subscripts.
🌐
Scaler
scaler.com › topics › void-pointer
What is a Void Pointer in C++? - Scaler Topics
August 12, 2022 - In this example, a void pointer is used to point to an integer, float, and string. The printData function takes a void pointer and an enum class DataType as parameters. Inside the function, the void pointer is explicitly converted to the appropriate ...
🌐
HackerEarth
hackerearth.com › practice › notes › void-pointer-in-c
void pointer in C | HackerEarth
int a = 10; char b = 'x'; void *p = &a; // void pointer holds address of int 'a' p = &b; // void pointer holds address of char 'b'
Top answer
1 of 4
161

A void* does not mean anything. It is a pointer, but the type that it points to is not known.

It's not that it can return "anything". A function that returns a void* generally is doing one of the following:

  • It is dealing in unformatted memory. This is what operator new and malloc return: a pointer to a block of memory of a certain size. Since the memory does not have a type (because it does not have a properly constructed object in it yet), it is typeless. IE: void.
  • It is an opaque handle; it references a created object without naming a specific type. Code that does this is generally poorly formed, since this is better done by forward declaring a struct/class and simply not providing a public definition for it. Because then, at least it has a real type.
  • It returns a pointer to storage that contains an object of a known type. However, that API is used to deal with objects of a wide variety of types, so the exact type that a particular call returns cannot be known at compile time. Therefore, there will be some documentation explaining when it stores which kinds of objects, and therefore which type you can safely cast it to.

This construct is nothing like dynamic or object in C#. Those tools actually know what the original type is; void* does not. This makes it far more dangerous than any of those, because it is very easy to get it wrong, and there's no way to ask if a particular usage is the right one.

And on a personal note, if you see code that uses void*'s "often", you should rethink what code you're looking at. void* usage, especially in C++, should be rare, used primary for dealing in raw memory.

2 of 4
44

Void is used as a keyword. The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type:

General syntax:

void* pointer_variable;

void *pVoid; // pVoid is a void pointer

A void pointer can point to objects of any data type:

int nValue;
float fValue;

struct Something
{
    int nValue;
    float fValue;
};

Something sValue;

void *pVoid;
pVoid = &nValue; // valid
pVoid = &fValue; // valid
pVoid = &sValue; // valid

However, because the void pointer does not know what type of object it is pointing to, it can not be dereferenced! Rather, the void pointer must first be explicitly cast to another pointer type before it is dereferenced.

int nValue = 5;
void *pVoid = &nValue;

// can not dereference pVoid because it is a void pointer

int *pInt = static_cast<int*>(pVoid); // cast from void* to int*

cout << *pInt << endl; // can dereference pInt

Source: link

Find elsewhere
🌐
BYJUS
byjus.com › gate › void-pointer-in-c
Void Pointer in C
August 1, 2022 - Initializing the u pointer to v. Print “The Integer variable in the program is equal to = ”. Printing the value of v using the pointer u. Initializing the u pointer to w. Print “The Float variable in the program is equal to = ”. Printing ...
🌐
Unstop
unstop.com › home › blog › void pointer in c explained in detail with code examples
Void Pointer In C Explained In Detail With Code Examples // Unstop
March 1, 2024 - Typecasting has to mostly be done explicitly by specifying the desired data type in parentheses before dereferencing the pointer. For example, (int *)ptr for an integer, (float *)ptr for a float, (char *)ptr for a character, etc.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › cpp-void-pointer
void Pointer in C++ - GeeksforGeeks
August 2, 2025 - ... // C++ program to demonstrate the need for void pointer #include <iostream> using namespace std; int main() { int* ptr; float f = 90.6; ptr = &f; // error cout << "The value of *ptr is : " << *ptr << endl; return 0; }
Top answer
1 of 11
253

A pointer to void is a "generic" pointer type. A void * can be converted to any other pointer type without an explicit cast. You cannot dereference a void * or do pointer arithmetic with it; you must convert it to a pointer to a complete data type first.

void * is often used in places where you need to be able to work with different pointer types in the same code. One commonly cited example is the library function qsort:

void qsort(void *base, size_t nmemb, size_t size, 
           int (*compar)(const void *, const void *));

base is the address of an array, nmemb is the number of elements in the array, size is the size of each element, and compar is a pointer to a function that compares two elements of the array. It gets called like so:

int iArr[10];
double dArr[30];
long lArr[50];
...
qsort(iArr, sizeof iArr/sizeof iArr[0], sizeof iArr[0], compareInt);
qsort(dArr, sizeof dArr/sizeof dArr[0], sizeof dArr[0], compareDouble);
qsort(lArr, sizeof lArr/sizeof lArr[0], sizeof lArr[0], compareLong);

The array expressions iArr, dArr, and lArr are implicitly converted from array types to pointer types in the function call, and each is implicitly converted from "pointer to int/double/long" to "pointer to void".

The comparison functions would look something like:

int compareInt(const void *lhs, const void *rhs)
{
  const int *x = lhs;  // convert void * to int * by assignment
  const int *y = rhs;

  if (*x > *y) return 1;
  if (*x == *y) return 0;
  return -1;
}

By accepting void *, qsort can work with arrays of any type.

The disadvantage of using void * is that you throw type safety out the window and into oncoming traffic. There's nothing to protect you from using the wrong comparison routine:

qsort(dArr, sizeof dArr/sizeof dArr[0], sizeof dArr[0], compareInt);

compareInt is expecting its arguments to be pointing to ints, but is actually working with doubles. There's no way to catch this problem at compile time; you'll just wind up with a missorted array.

2 of 11
30

Using a void * means that the function can take a pointer that doesn't need to be a specific type. For example, in socket functions, you have

send(void * pData, int nLength)

this means you can call it in many ways, for example

char * data = "blah";
send(data, strlen(data));

POINT p;
p.x = 1;
p.y = 2;
send(&p, sizeof(POINT));
🌐
EDUCBA
educba.com › home › software development › software development tutorials › c programming tutorial › void pointer in c
Void Pointer in C | Examples on How Void Pointer Work in C?
April 3, 2023 - This program is used to illustrate the dereferencing of the void pointer of C where the input is given to variable with the inception operator which is shown with the following example.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Programiz
programiz.com › cpp-programming › pointer-void
C++ Pointer to void (With Examples)
The output shows that the void pointer ptr stores the address of a float variable f.
🌐
Reddit
reddit.com › r/cpp_questions › void pointers
r/cpp_questions on Reddit: void pointers
October 13, 2020 -

Hey guys, I am getting familiar with different concepts of C++. I came across void pointers. I know that when we use malloc it returns a void pointer and we need to typecast it. What else is a void pointer used for and how can we create generic functions using void pointer?

🌐
Javatpoint
javatpoint.com › void-pointer-in-c
Void Pointer in C - javatpoint
Void Pointer in C with Tutorial or what is c programming, C language with programming examples for beginners and professionals covering concepts, control statements, c array, c pointers, c structures, c union, c strings and more.
🌐
Deardevices
deardevices.com › 2019 › 05 › 07 › void-pointers-part1
3 Practical Uses of void Pointers in the C Language (part 1/3)
In order to make a certain function ... Example: qsort from the C Standard Library. The qsort function is for sorting arrays of any type. The following snippet shows its signature: void qsort(void * base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); This actually demonstrates two different uses of void pointers...
🌐
OverIQ
overiq.com › c-programming-101 › void-pointers-in-c
Void Pointers in C - C Programming Tutorial - OverIQ.com
Before you dereference a void pointer it must be typecasted to appropriate pointer type. Let me show you what I mean. For example: In the above snippet void pointer vp is pointing to the address of integer variable a. So in this case vp is acting as a pointer to int or (int *).
🌐
PrepBytes
prepbytes.com › home › c programming › void pointer in c
Void Pointer in C
January 8, 2024 - Explanation In the above example, the swap() function is implemented using a void pointer. The function accepts two void pointers, ‘ptr1’ and ‘ptr2’, and a size_t variable ‘size’ that specifies the size of the data being swapped.
🌐
Javatpoint
javatpoint.com › cpp-void-pointer
C++ Void Pointer - javatpoint
C++ Void Pointer with C++ tutorial for beginners and professionals, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.
🌐
YouTube
youtube.com › codebeauty
C++ POINTERS (2020) - What is a void pointer? (for beginners) PROGRAMMING TUTORIAL - YouTube
Void pointer in C++ is a general-purpose pointer, which means a special pointer that can point to object of any type. It is a typeless pointer also known as ...
Published   September 30, 2020
Views   85K