I am trying to get a protobuf-c example compiled with a C90 compiler (MS VS2012).
Within the protobuf-c source code there are two C99 specific things that can easily be changed to be compatible with C90, i.e. variable declaration in the middle of a scope (not allowed in C90) and instantiation of structs via the .
-syntax (e.g. some_struct_type name = {.a=1,.b=2}
).
I am now stuck with one compile error left. The respective line in source file 'protobuf-c.c' reads:
void *array = *(void **) member + siz * (*p_n);
Where member
is define as void *
and p_n
as size_t *
.
And the respective error is
error C2036: 'void *' : unknown size
Please note this is valid for protobuf-c version 1.0.1 (see the respective source code, at line 2404). This line has been changed in version 1.0.2 to
void *array = *(char **) member + siz * (*p_n);
with this comment. Changing the line accordingly eliminates the compilation error.
My questions are:
*(char **)
version?(For some other reason I want to stick to protobuf-c 1.0.1)
Last Updated : 03 Jan, 2019 A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type. int a = 10;
What is void in C programming? It means “no type”, “no value” or “no parameters”, depending on the context. We use it to indicate that: a pointer does not have a specific type and could point to different types. Do you learn better from video?
It means “no type”, “no value” or “no parameters”, depending on the context. We use it to indicate that: a pointer does not have a specific type and could point to different types. This is probably the most used context of the void keyword. Here we use it as a return type of a function. Such function does not return a value.
function (void) – does not accept parameters In C, if you don’t specify the parameters of a function, it can accept any number of parameters of any type. If you come from another programming language, this could be confusing at first.
parameter passed as member
is an address of a pointer to char pointer . First the cast and defererence get that pointer.( The address of the pointer is passed so that the pointer can be changed in the function. )
The addition siz * (*p_n)
increments the pointer to the correct element.
The whole line could be rewritten as:
char** pm = member ;
char* m = *pm ;
void *array = m + siz * (*p_n);
A change from void* to char* is made so that pointer arithmetic is possible, as C Standard doesn't allow it on void pointers. The error message is telling you that, size of the object void* is pointing to is not known, there for you have to use a char* pointer.
As long as the object passed to the function has the type char**
you can switch to char** version.
I would like to understand this line of code.
The code makes pointer arithmetic and tries to hide the details. Therefore it uses void
everywhere the pointer is not deferenced. The assumption is that the sizeof (*(void*))
is equal to one.
Can I switch to the *(char **) version?
Yes, because the code assigns the resulting pointer and don't dereferences the pointer.
What is the error message telling me?
This hack is compiler specific. Your compiler doesn't undestands this, it does not provide a size of a void*
target.
You should use a "portable" version. The sizeof(char)
is one for most compilers and for these compilers it is portable.
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