Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

how to initialize members of an array of struct in C

I have the following struct:

typedef struct {
    int someArray[3][2];
    int someVar;
} myStruct;

If I create an array of this struct in my main (like the following), how would I initialize it?

int main() {
    myStruct foo[5];
}

I want to initialize the above array of struct in a way similar to initilazing a normal array (see below):

int main() {
    int someArray[5] = {1,4,0,8,2};
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Work from the outside in. You know you have an array of 5 things to initialize:

mystruct foo[5] = { 
                    X, 
                    X, 
                    X, 
                    X, 
                    X 
                  };

where X is a stand-in for initializers of type mystruct. So now we need to figure out what each X looks like. Each instance of mystruct has two elements, somearray and somevar, so you know your initializer for X will be structured like

X = { Y, Z }

Substituting back into the original declaration, we now get

mystruct foo[5] = { 
                    { Y, Z }, 
                    { Y, Z }, 
                    { Y, Z }, 
                    { Y, Z }, 
                    { Y, Z } 
                  };

Now we need to figure out what each Y looks like. Y corresponds to an initializer for a 3x2 array of int. Again, we can work from the outside in. You have an initializer for a 3-element array:

Y = { A, A, A }

where each array element is a 2-element array of int:

A = { I, I }

Subsituting back into Y, we get

Y = { { I, I }, { I, I }, { I, I } }

Substituting that back into X, we get

X = { { { I, I }, { I, I }, { I, I } }, Z }

which now gives us

mystruct foo[5] = {
                    { { { I, I }, { I, I }, { I, I } }, Z },
                    { { { I, I }, { I, I }, { I, I } }, Z },
                    { { { I, I }, { I, I }, { I, I } }, Z },
                    { { { I, I }, { I, I }, { I, I } }, Z },
                    { { { I, I }, { I, I }, { I, I } }, Z }
                  };

Since Z is a stand-in for an integer, we don't need to break it down any further. Just replace the Is and Zs with actual integer values, and you're done:

mystruct foo[5] = {
                    {{{101, 102}, {201, 202}, {301, 302}}, 41},
                    {{{111, 112}, {211, 212}, {311, 312}}, 42},
                    {{{121, 122}, {221, 222}, {321, 322}}, 43},
                    {{{131, 132}, {231, 232}, {331, 332}}, 44},
                    {{{141, 142}, {241, 242}, {341, 342}}, 45}
                  };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...