I'm trying to do some basic pointer arithmetic with a void *. My actual code computes an offset by using sizeof and then multiplying. Here is sample code to show an instance of the issue by itself.
void * p;
p = 0;
p = p + 1;
I'm using the MSVC compiler in C (not C++).
The error is:
expression must be a pointer to a complete object type
I'm not understanding what this error is trying to say. There is no object or struct here.
Since void is an incomplete type, it is not an object type. Therefore it is not a valid operand to an addition operation. Therefore you cannot perform pointer arithmetic on a void pointer.
You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.
You can not increment a void pointer. Since a void* is typeless, the compiler can not increment it and thus this does not happen.
Pointer arithmetic is always in terms of the size of the pointed-to object(s). Incrementing a char*
will advance the address by one, whereas for int*
it would usually be four (bytes). But void
has unknown size, so pointer arithmetic on void*
is not allowed by the standard. Cast to the appropriate type first; if you just want to manipulate the address as if it were a number then cast to char*
or use intptr_t
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With