![](/uploads/1/2/5/5/125558190/753077467.jpg)
As the mid-high element of the Incubus system, the Air Array's radical looks are purposefully designed to form a coherent radiation pattern, where the sound.
Active2 years, 3 months ago
plain C have nice feature - void type pointers, which can be used as pointer to any data type.
But, assume I have following struct:
But, assume I have following struct:
where value field may point to char array, or to int, or something else.
So when allocating new instance of this struct, I need:
So when allocating new instance of this struct, I need:
1) allocate memory for this struct;
2) allocate memory for value and assign it to value field.
2) allocate memory for value and assign it to value field.
My question is - is there ways to declare 'array of type void', which can be casted to any another type like void pointer?
All I want is to use 'flexible member array' (described in 6.7.2.1 of C99 standard) with ability to casting to any type.
Something like this:
I suppose declaring token->value as char or int array and casting to needed type later will do this work, but can be very confusing for someone who will read this code later.
Shweta2,2221111 gold badges3838 silver badges5555 bronze badges
S.J.S.J.75311 gold badge99 silver badges1414 bronze badges
5 Answers
Well, sort of, but it's probably not something you want:
The following macro can be used to index your data (assuming
x
is a struct token *
):And, if you like, the following macro can wrap your
make_token
function to make it a little more intuitive (or more hackish, if you think about it that way):Usage:
Chris LutzChris Lutz57.4k1414 gold badges114114 silver badges174174 bronze badges
I would probably do this:
edit, actually you have to typecast those p.value[index] = somethings. And/or use a union to not have to typecast.
51.3k77 gold badges6767 silver badges129129 bronze badges
You can't have an array of 'void' items, but you should be able to do something like what you want, as long as you know value_size when you do the malloc. But it won't be pretty.
Note that you need an extra address-of operator when you want to get the extra storage.
This will 'waste' sizeof(void*) bytes, and the original type of
AShellyAShellyvalue
doesn't really matter, so you may as well use a smaller item. I'd probably typedef char placeholder;
and make value
that type.27.2k1111 gold badges7676 silver badges131131 bronze badges
Expanding on AShelly's answer you can do this;
Note the use of 'offsetof()' instead of 'sizeof()' when allocating the memory to avoid wasting the 'void *buf;' field size. The type of 'buf' doesn't matter much, but using 'void *' means it will align the 'buf' field in the struct optimally for a pointer, adding padding before it if required. This usually gives better memory alignment for the entries, particularly if they are at least as big as a pointer.
Accessing the entries in the buffer looks like this;
Note the extra address-of operator to get the address of the 'buf' field as the starting point for the allocated entry memory.
Donovan BaardaDonovan Baarda
following structure can help you.
More Details : clibutils
AvinashAvinash5,7512323 gold badges8888 silver badges166166 bronze badges
Not the answer you're looking for? Browse other questions tagged carraysvoid-pointers or ask your own question.
![](/uploads/1/2/5/5/125558190/753077467.jpg)