r/C_Programming • u/SeaInformation8764 • May 20 '26
Question Why Don't Double void Pointers Match Like Single void Pointers?
In the following code, calling test1 doesn't provide a warning, but calling test2 does. Is there a specific reason for this behavior?
void test1(void* x);
void test2(void** x);
int main() {
int* x1;
int** x2;
test1(x1); // ok
test2(x2); // warning "incompatible pointer type"
}Edit: I think some people don't understand. I'm asking why void** doesn't match with other x**, not why it doesn't work exactly the same as void*.
36
Upvotes
54
u/Total-Box-5169 May 20 '26
In C void* is a generic pointer, void** is not.
18
u/dqUu3QlS May 20 '26
If you have a
void**, you can dereference and assign any pointer type to it:So if you could create a
void**from any double pointer type you could create an unsafe memory access in a sneaky way:6
u/not_a_bot_494 May 20 '26
void* is a pointer to an unknown type. void** is a pointer to a known type, namely a void*.
5
u/Certain-Flow-0 May 20 '26
C++ version of the same issue
https://isocpp.org/wiki/faq/proper-inheritance#derivedptrptr-to-baseptrptr
2
u/Conscious_Support176 May 20 '26 edited May 20 '26
Because void * is a special case. Why would you expect void** to behave like void*?
More explicitly, void * is a loophole in the type system to allow C to provide generic pointer functions without access to generics.
2
u/olorochi May 20 '26 edited May 20 '26
Because void** is not a void*, but a pointer to it, just like a int* is not an int. The void qualifier does not propagate like you expected. In void**, void* is just the type being pointed to by the pointer (last *).
Therefore, in your example, when you pass x2 to test2(void**), the compiler generates a warning because it expects a pointer to type void*, but instead finds one to int*. Passing x1, or any other pointer that does not specifically point to a void*, would cause the same warning.
If you changed the definition to just test2(void*), you would silence this warning, since int** is just like any other pointer type in that it implicitly casts to void*. The drawback is losing all type safety when you use that pointer: void** is more semantically meaningful than void*, as it guarantees, so long as you don't ignore warnings, that the function obtains a pointer to generic pointers, and not to an arbitrary type such as char.
1
u/jontsii May 20 '26
a void * points to an unkown datatype. A void ** points to a pointer that points to a void*. And an int ** points to a pointer that points to an int. That is why you can't pass an int ** to a function that takes a void ** as an argument,
1
u/flatfinger May 20 '26
Although C was designed on platforms where the smallest individually writable units of storage have distinct addresses, it is also usable on platforms where that is not the case. There are platforms where each address identifies e.g. a 16-bit or 36-bit chunk of storage, but there exist instructions that will load or store the nth 8-bit or 9-bit chunk starting at a particular address. On such a platform, an
int*would simply hold a word address, but achar*orvoid*would combine a word address and a character-based offset. If code were to take the address of anint*, convert it tovoid**, and attempt to store a pointer to the location identified thereby, such action would overwrite whatever followed theint*.It would be helpful if the Committee were willing to acknowledge features that implementations should support when practical. It may, for example, be useful to allow a programmer to write a variation of
qsort()which would assume that the things being sorted are pointers (thus avoiding the need for it to usememcpyormemmoveto swap elements) but was agnostic with the type of those pointers. Unfortunately, ever since 1989 the language has been stuck with a philosophy that rather than allow implementations for quirky targets to impose limitations based upon those targets, it's somehow better to impose such limitations on all programmers whether or not anyone would ever want to use their programs on such quirky targets.1
u/WormHeamer May 20 '26 edited May 20 '26
The way I'd heard it explained: Some architectures are word-addressed instead of byte-addressed, which requires
char*to be bigger thanint*in order to specify the byte being referenced. Becausevoid*needs to be able to target the address of all data types, includingchar(though I believe not function pointers since those are so variable across platforms), it will also be that size. As a result, if we have something like:Then
is_compatibleshould return false on those machines, because the memory offsets of a nonzero index into an array ofint*and an array ofvoid*don't line up.1
u/NoSpite4410 May 21 '26
The function test1(void*) casts a pointer to a raw (untyped) pointer upon entry. The idea is you are going to cast the pointer to its proper type inside the function. It is assumed you know what type the pointer was before you stripped off its type as a parameter. A good example is qsort
qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));The second function
test2(void** x)explicitly specifies "pointer to void pointer" , which is a pointer to a pointer to an untyped byte. Passing int** to it is passing "pointer to int pointer" which is a pointer to a pointer of a 4-byte type. The compiler flags that as a probably mistake, which it usually is when you pass a pointer to a different size than expected.Now you pass a double pointer most of the time as an array of arrays, or sometimes as a pointer to something you are going to allocate on the heap. The double pointer allows modification of the address of the pointer involved, which a single pointer does not when passed as a parameter. In all cases the identifier contains the same address it did when the function returns. So passing a single pointer allows for read/write of existing address, but not attaching a new allocation. A double pointer does.
0
u/Khipu28 May 20 '26
test1(x2) should work. 🤯
-1
u/somewhereAtC May 20 '26
The aren't the same. The first is a pointer to void. The second is a pointer to a pointer (which happens to point to void).
-3
u/zhivago May 20 '26
For the same reason that
doubleis not the same asdouble *.-3
u/RRumpleTeazzer May 20 '26
void* in C is a huge mistake.