Reputation: 107
I know you can't utilize pointer arithmetic on void pointers, but could you theoretically do pointer arithmetic on pointers to void pointers, since sizeof(void *)
would yield an answer of how many bytes a pointer takes on your system?
Upvotes: 5
Views: 188
Reputation: 26800
Pointer arithmetic is not permitted on void*
because void
is an incomplete object type.
From C Committee draft N1570:
6.5.6 Additive operators
...
2. For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type.
But it is permitted on void**
because void*
is NOT an incomplete object type. It is like a pointer to a character type.
6.2.5 Types
...
19. The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.
...
28. A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.
Upvotes: 9
Reputation: 58848
Yes, pointer arithmetic works on pointers to void pointers (void**
). Only void*
is special, void**
isn't.
Example:
void *arrayOfVoidPtr[10];
void **second = &arrayOfVoidPtr[1];
void **fifth = second + 3; // pointer arithmetic
Upvotes: 6