void* vs. char* pointer arithmetic
The accepted answer is a good summary and I want to make it more clear why to use one or another. ;)
Although (void *) and (char *) can be both equivalently cast to any other pointer type, it is only legal to perform pointer arithmetic on a (char *) and not with (void *) if you want to comply with Standard C.
Why both are used as pointers ? Most pointer conversions to and from (void *) can be done without a cast but the use of (char *) is a reminiscence of the old times.
GCC does not warn about pointer arithmetic on (void *) as it is not a compiler intended to be compliant with standard C but for GNU C. To behave in compliance with standard C you can use the following flags:
gcc -ansi -pedantic -Wall -Wextra -Werror <more args...>
My bottom line:
- If you want to perform pointer arithmetic: use (char *)
- If you want to get the pointer address in order to cast it into another type later on: use (void *)
It's a slip-up. Arithmetic on void *
is not defined by the standard, but some compilers offer it as an extension, behaving the same as char *
for arithmetic. The second is formally not valid C, but slipped through presumably out of (bad) habit.