I think what I am looking for is actually not possible in C, but maybe some has an idea how to work around it:
I need to process some input data. This data is given in an int with gives the number of data and a number of strings (i.e. char *) which hold the actual data. These strings are named data_0 … data_n:
int n = 42; // the number of strings
char *data_0 = "some input1";
char *data_1 = "some input2";
....
char *data_41 = "the last input data";
So this is how I get the data. The question now is: How can I process it? My goal is to store them in a big array. Initializing this array is of course simple, I just need an array of n char-Pointer which I get with malloc.
But then I want to assign these strings into the array. And this is the point where I’m stuck. I need to assign them dynamically, as I do not know the size before.
Something like:
for(i = 0; i < n; i++)
datastorage[i] = data_i;
So I mean accessing the variablename dynamically. I hope you understand what I mean 🙂 Thank you.
Make it an array in the first place! There is absolutely no point in having 42 separate variables if you have to access them as an array! BTW, in C, variable names are not available at run time, they are simply lost after compilation, so forget about dynamically accessing variables by name.