I did try searching for this on Stack Overflow, but I think due to the syntax and not knowing exactly what to search I became a little unstuck.
I have seen (void*)
being used to cast, usually to function calls. What is this used for?
Any type of pointer can be assigned to (or compared with) a void pointer, without casting the pointer explicitly. Finally, a function call can be cast to void in order to explicitly discard a return value. For example, printf returns a value, but it is seldom used.
Casting to void is used to suppress compiler warnings. The Standard says in §5.2. 9/4 says, Any expression can be explicitly converted to type “cv void.” The expression value is discarded. Follow this answer to receive notifications.
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.
A void pointer is a pointer that can point to any type of object, but does not know what type of object it points to. A void pointer must be explicitly cast into another type of pointer to perform indirection.
void*
, usually referred to as a void pointer, is a generic pointer type that can point to an object of any type. Pointers to different types of objects are pretty much the same in memory and so you can use void pointers to avoid type checking, which would be useful when writing functions that handle multiple data types.
Void pointers are more useful with C than C++. You should normally avoid using void pointers and use function overloading or templates instead. Type checking is a good thing!
A void pointer is a pointer to a value whose type and size are unknown. I'll give an example of how it might be used in C - although other commenters are correct in saying that it by and large shouldn't be used (and, in fact, isn't necessary) in C++.
Suppose you have a struct which stores a key and a value. It might be defined like this:
typedef struct item_t
{
char *key;
int value;
} item_t;
So you could use this structure to match up any string key with an integer value. But what if you want to store some arbitrary value? Maybe you want some keys to match up with integers, and some keys to match up with doubles? You need to define your struct in such a way that it will keep track of (with a pointer) a value of arbitrary type and size. The void *
fits the bill in this case:
typedef struct item_t
{
char *key;
void *value;
} item_t;
Now, if your value is an int, you can get at its value using (int *) value
, or if its value is a double, you can do it using (double *) value
. This assumes, of course, that the code using the struct knows what type it expects to see in what context.
In C++, though, this same functionality is usually achieved not through the use of void *
, but by using things like templates or a superclass which all of your different types descend from (like Object in Java, for example - a list of Objects can store some String's, some Integer's, etc.).
It's the most generic way of providing a pointer to an object in C/C++. Like object
in C# or Java.
That is mostly used when you want to pass a parameter that can have different states.
For example, in your generic code you would pass the void*
as a parameter, and then in the "specific" implementation of that code, you would cast the void*
to whatever you want
Generic code, abc.h
void Test(void* pHandle);
Windows code, abc.cpp
void Test(void* phandle)
{
WindowsStructure* myStruct = (WindowsStructure*)(pHandle);
myStruct->getWhatINeed;
}
Linux code, abc.cpp
void Test(void* phandle)
{
LinuxStructure* myStruct = (LinuxStructure*)(pHandle);
myStruct->getWhatINeed;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With