Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression must be a pointer to a complete object type using simple pointer arithmetic [duplicate]

Tags:

c

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.

like image 598
101010 Avatar asked Jun 29 '14 02:06

101010


People also ask

How do you do pointer arithmetic on void pointers?

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.

How to print pointer value c++?

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.

Can you increment a void pointer in C?

You can not increment a void pointer. Since a void* is typeless, the compiler can not increment it and thus this does not happen.


1 Answers

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.

like image 107
John Zwinck Avatar answered Sep 25 '22 10:09

John Zwinck