There are a number of global variables, let's say for example someglobalvalue1, someglobalvalue2, etc, that are updated dynamically from a different part of the app I'm working on. In the part that I am currently working on, I need to gather these variables into an integer array. Because there are a lot of these variables in different groups, I have separate functions like GetGroupOne(), GetGroupTwo(), etc to insert the globals into an array and return it to the main function. I'm using this page as a guide for how to return int arrays from functions: http://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm
So for example:
int main() {
int *array;
array = GetGroupOne();
/* do stuff with array */
return 0;
}
int * GetGroupOne() {
static int array[GROUP_ONE_LENGTH] = { someglobalvalue1, someglobalvalue2, someglobalvalue3 };
return array;
}
So, trying to compile this (by the way, I am limited to a C90 compiler) gets me this error:
Error “initializer element is not constant” when trying to initialize variable with const
So, I did find Error "initializer element is not constant" when trying to initialize variable with const and other threads that seemed to suggest that what I was trying to do, initialize using globals, is not possible. So I bit the bullet and did this instead:
int * GetGroupOne() {
static int array[GROUP_ONE_LENGTH];
array[0] = someglobalvalue1;
array[1] = someglobalvalue2;
array[2] = someglobalvalue3;
/* etc... */
return array;
}
Now, this works but it is incredibly ugly and it hurts my soul to look at it. Especially because there are multiple groups, some of which have over a hundred entries, so I have to insert each global into the array individually. I know how I would handle this in higher level languages, but C is still somewhat of a mystery to me. But I'm thinking there MUST be some better way to handle this problem. Can anyone nudge me in the right direction?
Thank you.