void pointers are common practice in C, and rarely used in C++. And in C, you don't have to use a typecast to turn a void * into any other kind of data pointer. In C, they are used for creating generic functions. If you look at qsort, memcpy, memmove, or memset, they all use void * to make their interfaces generic. And qsort expects you to pass in a genericized comparison function that takes void * arguments. BTW, the C++ equivalents for the C functions qsort, memcpy, memmove, and memset are ::std::sort, ::std::copy, ::std::move, and ::std::fill respectively. Notice how the C++ equivalents are made generic with templates, which preserve type information. The problem here (and the reason void pointers aren't common in C++) is how type information disappears. The compiler can't help you to avoid passing in, say, a string comparison function when you're trying to sort records that each contain 3 coordinates expressed as floats. You just have to see the nonsensical result or crash to notice the problem. Answer from Omnifarious0 on reddit.com
🌐
GeeksforGeeks
geeksforgeeks.org › c language › void-pointer-c-cpp
void Pointer in C - GeeksforGeeks
If you're looking to master pointers, including how they work with data structures, the C Programming Course Online with Data Structures covers pointers extensively with practical use cases. ... The following program doesn't compile. ... // C Program to demonstrate that a void pointer // cannot be dereferenced #include <stdio.h> int main() { int a = 10; void* ptr = &a; printf("%d", *ptr); return 0; }
Published   3 weeks ago
🌐
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?

Discussions

What is a void pointer in C++? - Stack Overflow
Possible Duplicate: What is a void pointer and what is a null pointer? I often see code which resembles something like the following: void * foo(int bar); What does this mean? Does it mean th... More on stackoverflow.com
🌐 stackoverflow.com
Double void pointer gives "incompatible pointer types"
The type int * is not compatible with void ** because int is not compatible with void *, in turn because int is not a pointer type. More on reddit.com
🌐 r/C_Programming
8
1
November 18, 2020
Converting void pointers to function pointers.
In straight C it's technically not permitted, though the specification lists it as a common extension. Function pointers are not necessarily compatible with object or void pointers. In POSIX, this cast is permitted and valid since it's needed for dlsym(). More on reddit.com
🌐 r/C_Programming
4
6
October 30, 2016
void pointers in generic linked list
void * is a pointer type. You shouldn't use it to store integers. More on reddit.com
🌐 r/C_Programming
9
2
November 2, 2020
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
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
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
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_void_pointer.htm
void Pointer in C
A void pointer in C is a type of pointer that is not associated with any data type. A void pointer can hold an address of any type and can be typecasted to any type. They are also called general-purpose or generic pointers.
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Void-Pointers.html
Void Pointers (GNU C Language Manual)
The functions for dynamic memory allocation (see Dynamic Memory Allocation) use type void * to refer to blocks of memory, regardless of what sort of data the program stores in those blocks. With type void *, you can pass the pointer around and test whether it is null.
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

🌐
Quora
quora.com › What-is-the-purpose-of-void-pointers-and-why-are-they-used-instead-of-normal-pointers
What is the purpose of 'void' pointers and why are they used instead of normal pointers? - Quora
Answer: void pointers can contain any type. They are risky to use and therefore, their use limited. Before they are used for anything they have to be cast to the correct type. This is the risky part, if you cast them incorrectly bad things can happen. The only time I deal with void pointers is w...
Find elsewhere
🌐
HackerEarth
hackerearth.com › practice › notes › void-pointer-in-c
void pointer in C | HackerEarth
A void pointer can hold address of any type and can be typcasted to any type. 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'
🌐
BYJUS
byjus.com › gate › void-pointer-in-c
Void Pointer in C
August 1, 2022 - 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 that it points to the address of variables. It is also called the general purpose pointer.
🌐
Deardevices
deardevices.com › 2019 › 05 › 07 › void-pointers-part1
3 Practical Uses of void Pointers in the C Language (part 1/3)
May 7, 2019 - You can take the address of a certain variable and use a void pointer to store it: ... This makes sense: The compiler cannot infer what type of value is stored at the memory location referred by p. For instance, it doesn’t know how big that hunk of memory may be.
🌐
W3Schools
w3schools.com › c › c_pointers.php
C Pointers
In the example above, we used the pointer variable to get the memory address of a variable (used together with the & reference operator). You can also get the value of the variable the pointer points to, by using the * operator (the dereference operator):
🌐
BtechSmartClass
btechsmartclass.com › c_programming › C-Pointers-to-void.html
C Tutorials -void Pointer in C Programming Language
In C programming language, a void pointer is a pointer variable used to store the address of a variable of any datatype. That means single void pointer can be used to store the address of integer variable, float variable, character variable, double variable or any structure variable.
🌐
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 - A void pointer in C is a pointer that can represent variables of multiple data types. This is why when dereferencing void pointer, we must typecast it first.
🌐
Medium
medium.com › @ayushkapri.richard › generic-pointers-void-pointers-in-c-c-8541d51bea3e
Generic Pointers (Void Pointers) in C / C++. | by Ayush Kapri | Medium
May 4, 2024 - A void pointer (void *) is a special type of pointer in C and C++ that is used to point to data of unspecified type. It is often used…
🌐
Phipps Electronics
phippselectronics.com › home › blog › c tutor: what is a void pointer
C Tutor: What is a Void Pointer - Phipps Electronics
July 17, 2024 - You declare a void pointer variable using the void * type which means there is no actual type. Just like pointers, you can assign a memory location to void pointers that contains an actual value.
🌐
OverIQ
overiq.com › c-programming-101 › void-pointers-in-c › index.html
Void Pointers in C - C Programming Tutorial - OverIQ.com
We have learned in chapter Pointer Basics in C that if a pointer is of type pointer to int or (int *) then it can hold the address of the variable of type int only. It would be incorrect, if we assign an address of a float variable to a pointer of type pointer to int. But void pointer is an exception to this rule.
🌐
Testbook
testbook.com › home › gate › understanding void pointer in c programming - testbook.com
Understanding Void Pointer in C Programming - Testbook.com
In this syntax, the void keyword functions as the pointer type, followed by the pointer name, which the pointer type points to and allocates the address location in the code. The declaration of a pointer takes place with the name and type of ...
🌐
GitConnected
levelup.gitconnected.com › demystifying-void-pointers-a58c868fdb48
Demystifying void pointers in C. Why do we need void pointers in C … | by Alex K | Level Up Coding
July 4, 2023 - it takes two arguments const void *, basically we can pass there any type because of void *ptr, but inside the function, we are casting input pointers to specific data type — pointer to person structure and accessing its member age:
🌐
Quora
quora.com › Can-you-explain-the-difference-between-void-and-other-pointers-in-C
Can you explain the difference between void* and other pointers in C? - Quora
A pointer to void (data type void *) is a pointer which can contain an address, just like any other pointer, but it can be a pointer to something of any data type, and the compiler doesn’t know what data...
🌐
Wikipedia
en.wikipedia.org › wiki › Void_type
Void type - Wikipedia
2 weeks ago - It is also similar to the unit type used in functional programming languages and type theory. See Unit type#In programming languages for a comparison. C and C++ also support the void pointer (or pointer to void type), denoted void*, but this is an unrelated notion.