5

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?

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The second quote is not really relevant; the rules for pointer arithmetic to complete types apply
Just wanted to show that a pointer to void is a complete type.
6

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

1 Comment

Some compilers allow as an extension void * arithmetics. One of the examples is gcc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.