I am trying to create a generic (or untyped) array in C (I know that C ++ makes this easier). In a nutshell, I want to allocate an array to store an array of a certain known type (at run time). In a real implementation, this depends on user input.
I try to use the enum / struct script, following the tips found in several google hits, but I'm afraid that my inexperience with void pointers and the lack of a concrete example prevent me from getting a working piece of code, (Normally I would just buy a book, but I'm in the country where I don’t speak the language, and books on programming in English do not exist).
The problem boils down to the following simplification: I have an image (only 1D), the pixel value can be int, float or double. I have a function that will tell me the type. All I want to do is save the pixels in an array of the appropriate type. (In practice, these images are very large, and my motivation is to save memory and prevent writing blocks of code for each type.)
I tried something like the following, but maybe it's not the best (code snippet for a possible data structure):
enum type {
typeint, typefloat, typedouble
};
struct genericarray {
enum type type;
void *storage;
};
Somehow I want to store these pixels in instances of generciarray. All my attempts so far have turned into a "warning: playing out the" void * pointer "of the parties, which I admit that I do not understand.
, , , . for-loops init. , .