5
Error    1    error C2036: 'const void *' : unknown size    file.cpp     111

I don't follow. GCC never complains about void * pointer arithmetic, even on -ansi -pedantic -Wall. What's the problem?

Here's the code-

struct MyStruct {

    const void *buf;    // Pointer to buffer  
    const void *bufpos; // Pointer to current position in buffer

};

...

size_t    someSize_t, anotherSize_t;
MyStruct *myStruct = (MyStruct *) userdata;
...
  if ( (myStruct->bufpos + someSize_t) > 
       (myStruct->buf + anotherSize_t) ) { // Error on this line
     ...
Braiam
  • 1
  • 11
  • 47
  • 78
  • 1
    Some of those "..." sections might be important. I can't tell what you're doing without seeing more code. But maybe http://msdn.microsoft.com/en-us/library/1kay26wa%28VS.80%29.aspx can help you. – jamesdlin Aug 17 '10 at 02:18
  • possible duplicate of [Pointer arithmetic for void pointer in C](http://stackoverflow.com/questions/3523145/pointer-arithmetic-for-void-pointer-in-c) – sashoalm Mar 06 '14 at 15:26
  • `gcc` accepts that `sizeof(void) == 1` but it's an "extension" of the standard. – Jean-François Fabre Mar 27 '18 at 12:48

4 Answers4

12

You can't do pointer math on a void * pointer. Cast oData->bufpos and oData->anotherConstVoidPtr to something the compiler knows how to deal with. Since you seem to be looking for sizes, which are presumably in bytes, casting to char * should work:

if (((char *)oData->bufpos + someSize_t) ...
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 4
    Or given that `bufpos` points to a series of bytes, change it to type `char*`. `void*` is best used for opaque object handles where you don't even want to allow byte-wise access. – Potatoswatter Aug 17 '10 at 02:34
  • Hi Carl Norum In the following line of code: strm.next_out = [compressed mutableBytes] + strm.total_out; Error is : "Arithmetic on a pointer to void" . Please Guide me how to do find the solution to this @Carl Norum – Babul Oct 25 '12 at 13:53
  • @Babul, same problem, same solution. Add an appropriate typecast. – Carl Norum Oct 25 '12 at 15:56
5

On the line:

if ( oData->bufpos ...

The type of bufpos is still void*. The compiler doesn't know what that pointer points to, so it gives you that error.

For pointer arithmetic, void* has no size, so taking an offset, or doing other pointer arithmetic doesn't make sense. Cast it to char* if you want to offset it by a number of bytes:

if(((char*)oData->bufpos) + offset ...

Edited after more code/context was given

If you can help it, try to use char* instead of void*. People in C-land will know what you are talking about, because chars are bytes, and you'll save yourself the headache of casting.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • 1
    Why does Visual C++ break on this when rational C compilers don't? Why break the standard? – A Student at a University Aug 17 '10 at 12:56
  • @A Student at a University: I can only speculate, however I think your questions are loaded. It may not be standard (I haven't looked at the standard, but it seems accepted knowledge that you can't take an offset from a void pointer, because the size of the target is unknown). If it isn't the standard, than are other compilers actually that rational? If it is the standard, and VC++ doesn't conform, I can still see why they would make that mistake, because void pointers aren't generally used that way by a good portion of C users - char pointers are. – Merlyn Morgan-Graham Aug 17 '10 at 20:25
  • @A Student at a University: Besides that, are you compiling in MSVC in C++ or C? The extension of the file makes a huge difference in how the compiler works. For example, C++ is not a superset of C, though it tries to conform where it can. And MSVC does not implement C99 standards in its C compiler, only an earlier standard of C. – Merlyn Morgan-Graham Aug 17 '10 at 20:26
1

$3.9.1/9- The void type has an empty set of values. The void type is an incomplete type that cannot be completed. It is used as the return type for functions that do not return a value. Any expression can be explicitly converted to type cv void (5.4). An expression of type void shall be used only as an expression statement (6.2), as an operand of a comma expression (5.18), as a second or third operand of ?: (5.16), as the operand of typeid, or as the expression in a return statement (6.6.3) for a function with the return type void.

I suspect an improper use of 'void' beyond what is allowed by the Standard.

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
1

It is really old post but even Visual Studio 2022 supports C11 and c17 MSVC is returning error if you try to add size to void pointer address but for GCC that is totally fine.

void* array_get_ref(const arr_t* this, size_t index)
{
    return this->buffer + (index * this->item_size);
}

To solve problem on MSVC you need to cast void pointer to for example char* like this and it will work fine.

void* array_get_ref(const arr_t* this, size_t index)
{
    return (char*)this->buffer + (index * this->item_size);
}

If we think it about: any pointer is giving us memory address, so begin of array in this case and we just need to add byte offset , index * item_size which is stored to struct when we created array. (casting does nothing in this case just tricks MSVC compiler)

DevDad
  • 11
  • 2