The most efficient way to initialize array member of struct?

The only time an array variable can be initialized in this manner:

int someInt[MAXLINES] = {0};

Is during declaration.

But because this particular variable int lines[MAXLINES]; is declared within the confines of struct, which does not allow members to be initialized, the opportunity is lost to that method, requiring it to be initialized after the fact, and using a different method.

The most common (and preferred) way to initialize after declaration in this case is to use:

//preferred
memset(p->lines, 0, sizeof(p->lines));

A more arduous method, and one that is seen often, sets each element to the desired value in a loop:

for(int i=0;i<MAXLINES;i++)
{
    p->lines[i] = 0;
}

As noted in comments, this method will be reduced by a good optimizing compiler to the equivalent of an memset() statement anyway.


Arrays cannot be assigned to directly. You need to either use a loop to set all fields to 0 or you can use memset:

memset(p->lines, 0, sizeof(p->lines));

Note that for non-char types you can only to do this to set all members to 0. For any other value you need a loop.


If you want to use the = operator, you can do it this way:

struct wnode wn, *p;
/* ........ */
wn = (struct wnode){.word = wn.word, .lines = {0,}, .left = wn.left, .right = wn.right};
*p = (struct wnode){.word = p ->word, .lines = {0,}, .left = p -> left, .right = p -> right};

= {0} works only on initialization. You can't use it with assignment as such which is why you get the error.

You can either use a for loop as you said or use memset to zero out the array:

memset(p -> lines, 0, sizeof(p -> lines))

Tags:

C