Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of void *array = *(void **) member + siz * (*p_n);

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:

  • I would like to understand this line of code.
  • Can I switch to the *(char **) version?
  • What is the error message telling me?

(For some other reason I want to stick to protobuf-c 1.0.1)

like image 812
georg Avatar asked Oct 21 '14 13:10

georg


People also ask

What is a void pointer?

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?

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?

What does “void” mean in Python?

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.

What does void function not accept parameters mean?

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.


2 Answers

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.

like image 64
2501 Avatar answered Sep 22 '22 05:09

2501


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.

like image 33
harper Avatar answered Sep 18 '22 05:09

harper