0

I need to initialize an array of structs with the same default values. This is a VERY large array, so setting each element by hand in an initializer is not feasable. Is the following code a correct and sane way to do this, or will I need to fall back on some initializer function and a for loop?

#define SIZE_OF_S1_ARR 10000 //just some arbitrary size for an example

typedef struct { char* id, char* description} S1;

/*
 * Array of structs, with each element having an id and a description 
 * which is an empty c-string
/*/
S1 s1_arr[SIZE_OF_S1_ARR] = {{ "", "" }};

I will add that this array already existed as a char array which only contained the ids as a single character. I am replacing it with the more useful struct.

5
  • Why don't you wrap access to this array and return a default value if NULL is found? Commented May 23, 2013 at 15:48
  • Legacy code with lots of global variable nastiness. Commented May 23, 2013 at 15:49
  • The "universal zero initializer" works: S1 s1_arr[SIZE_OF_S1_ARR] = {0}; Commented May 23, 2013 at 16:03
  • 1
    S1 s1_arr[SIZE_OF_S1_ARR] = {0}; stackoverflow.com/questions/1920430/c-array-initialization Commented May 23, 2013 at 19:02
  • That works in this specific case, but what about with non-empty strings (such as an id of "_")? Commented May 24, 2013 at 15:33

3 Answers 3

1

In standard C there is not array initiaization other than 0, unless you specify each value separately.

However, if you use the GNU C compiler, you can use something like this:

char s1_arr[SIZE_OF_S1_ARR] = {[0 ... SIZE_OF_S1_ARR-1] = '_' };

Please note that this approach is not portable. In addition, please note that initalizing strings (i.e., pointer to char) to have the same value can be done in two ways: (possible automatically) allocate a number of stings and assign them or allocate one string and assign it to all elements

Sign up to request clarification or add additional context in comments.

1 Comment

Though it's a bit late this does seem to be the best solution and possibly the only "real" answer to my question. It is unfortunate that it is not a portable solution.
-1

It seems you are looking for the memset function.

3 Comments

That is helpful if I am forced to create an initialization function, however I'd rather have this set by the compiler if possible. I do understand this simply may not be a feature of the language, but I am hoping there is a way.
look at this question: stackoverflow.com/questions/9669206/… your code would look very odd for 10000 items :)
I had seen that, however I was hoping against hope that there was another way. I will leave this question up while I work on an initialization function in case someone else can come up a static initialization solution.
-2

To initialize your S1 type

#define ARBITER 10000 // just an example number for elements

S1 *s1_arr = (S1*)malloc(sizeof(S1)*ARBITER);

id and description are initialized with NULL.

To set s1_arr->id and s1_arr->description you need also a *char variable

or just a literal string (char* a_string = "I am a String"; // This is a literal).

Hope I help you out.

1 Comment

malloc does NOT initialize the memory! for that u need to use cmalloc() man7.org/linux/man-pages/man3/malloc.3.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.