When is casting void pointer needed in C?
c, casting, pointers, void
Solution
No need to cast from or to a pointer to `void` in C:
6.3.2.3 Pointers
1 A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
The only exceptions from this are
- when printing a pointer using the `"%p"` conversion specifier as it's only defined for `void *`.
- when copying a pointer's value from an `intptr_t` or `uintptr_t` back to a `void *`.
Problem
I've been looking at Advanced Linux Programming by Mitchell, Oldham and Samuel. I've seen in the section on pthreads something about void pointers and casting that confuses me. Passing an argument to pthread_create(), they don't cast the pointer to a void pointer even though that is what the function expects. ``` pthread_create( &thread, NULL, &compute_prime, &which_prime ); ``` Here, which_prime is of type int. But taking a value returned from the thread using pthread_join, they DO cast the variable to void pointer. ``` pthread_join( thread, (void*) &prime ); ``` Here, prime is of type int again. Why is casting done in the second instance and not in the first?