The questioner has modified the question, and I have added some points. Based on my knowledge, I am not sure whether the sentence after the questioner is correct
Use void* just to "look" logical
However, going back to the above example, if I use float* instead of void*, that is indeed possible, but why do I have to do a cast type conversion swap((float* )&ai, (float* )&bi, sizeof(int)) every time I call it? You put such a If the interface is used by another person, what will he think? Damn it, if you exchange two integers, you have to convert it to float* first. If you give it to a newbie and they don’t dare to use it, Damn, do you have any secrets here?
Indeed, void* and other pointers occupy the same size of memory space. The only difference from other pointers is that you cannot dereference void*. It seems that other types of pointers can be used void* Instead, just convert it to a pointer of the corresponding type before dereferencing and you're good to go!
Well, if that’s the case, then using void* is indeed to look logical. I can’t think of any situations where void* must be used!
void * is mainly used for abstract data structures. Its specific uses include the following (not limited to):
as variant, such as setsockopt;
Hidden specific implementation (ADT): http://en.wikipedia.org/wiki/Abstract_data_type#Example:_implementation_of_the_stack_ADT
As the userdata/opaque of the callback interface, the userdata/opaque is passed as a parameter to the callback processing function. You can think of this as a Context.
void* can actually be understood as a context, which can be used to implement C language object-oriented programming. I think using void* is not to look appropriate, but a convention. When people who program in C/C++ see void*, they will habitually think that this is private data, and only the party that defined it has the right to interpret it. ;This is also the basic consensus that we pass void* as userdata in the callback function.
1. The sizeof of different types of pointers is the same. When allocating memory, if you don’t know whether the memory is int or float, you can let void* point to this memory and force transfer when using it.
2.int * is passed to the inside of the function as a parameter. It is not used inside the function for the time being. A warning will be issued during compilation. At this time, you can change it and it will not warn.
3. The number of bytes skipped by different types of pointers +1 is different.
There is already a correct answer. It is recommended to read stanford cs 107b, which explains this aspect in detail.
In addition, so-called "modern languages" such as C++ and Java will generally help you provide generic mechanisms in the language itself. Type cast is considered bad in most cases.
void*
Pointers can actually be used as generics. Imagine you want to exchange two variables in C. If they are two integers, it would be like:If you want to exchange decimals, you need to write another
So since the bit patterns of the two variables are exchanged, this method can be abstracted:
Because you don’t know how many bits to exchange, you also need a parameter to specify the number of bits to exchange:
With such a function, if you want to exchange two variables, you can:
The questioner has modified the question, and I have added some points. Based on my knowledge, I am not sure whether the sentence after the questioner is correct
However, going back to the above example, if I use
float*
instead ofvoid*
, that is indeed possible, but why do I have to do a cast type conversionswap((float* )&ai, (float* )&bi, sizeof(int))
every time I call it? You put such a If the interface is used by another person, what will he think? Damn it, if you exchange two integers, you have to convert it tofloat*
first. If you give it to a newbie and they don’t dare to use it, Damn, do you have any secrets here?Indeed,
void*
and other pointers occupy the same size of memory space. The only difference from other pointers is that you cannot dereferencevoid*
. It seems that other types of pointers can be usedvoid*
Instead, just convert it to a pointer of the corresponding type before dereferencing and you're good to go!Well, if that’s the case, then using
void*
is indeed to look logical. I can’t think of any situations wherevoid*
must be used!void *
is mainly used for abstract data structures. Its specific uses include the following (not limited to):variant
, such assetsockopt
;void* can actually be understood as a context, which can be used to implement C language object-oriented programming. I think using void* is not to look appropriate, but a convention. When people who program in C/C++ see void*, they will habitually think that this is private data, and only the party that defined it has the right to interpret it. ;This is also the basic consensus that we pass void* as userdata in the callback function.
1. The sizeof of different types of pointers is the same. When allocating memory, if you don’t know whether the memory is int or float, you can let void* point to this memory and force transfer when using it.
2.int * is passed to the inside of the function as a parameter. It is not used inside the function for the time being. A warning will be issued during compilation. At this time, you can change it and it will not warn.
3. The number of bytes skipped by different types of pointers +1 is different.
There is already a correct answer. It is recommended to read stanford cs 107b, which explains this aspect in detail.
In addition, so-called "modern languages" such as C++ and Java will generally help you provide generic mechanisms in the language itself. Type cast is considered bad in most cases.