如何初始化结构的C中的数组成员 [英] how to initialize members of an array of struct in C

查看:107
本文介绍了如何初始化结构的C中的数组成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构:

  typedef结构{
    INT的someArray [3] [2];
    INT someVar;
} MYSTRUCT;

如果我创建这个结构在​​我的主数组(如下面的),我将如何初始化呢?

  INT的main(){
    MYSTRUCT foo的[5];
}

欲初始化结构以类似于initilazing正常阵列的方式在上述阵列(见下面):

  INT的main(){
    INT的someArray [5] = {1,4,0​​,8,2};
}


解决方案

从外面你知道你有5件事情的数组初始化工作:

  MYSTRUCT富[5] = {
                    X,
                    X,
                    X,
                    X,
                    X
                  };

其中, X 是一个替身类型的初始化 MYSTRUCT 。所以,现在我们需要找出每个 X 的样子。 MYSTRUCT 的每个实例有两个元素,的someArray somevar ,所以你知道你的 X初始化的结构将像

  X = {Y,Z}

代回原来的声明,我们现在得到

  MYSTRUCT富[5] = {
                    {Y,Z},
                    {Y,Z},
                    {Y,Z},
                    {Y,Z},
                    {Y,Z}
                  };

现在,我们需要弄清楚每个Y样子。 Y对应于一个初始化为 INT 的3x2的阵列。同样,我们可以从外面的工作,您有一个3元素数组的初始化:

  Y = {A,A,A}

其中每个数组元素是一个2元素的数组 INT

  A = {I,I}

Subsituting回Y,我们得到

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

这代回X,我们得到

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

现在让我们

  MYSTRUCT富[5] = {
                    {{{I,I},{I,I},{我,我}},Z},
                    {{{I,I},{I,I},{我,我}},Z},
                    {{{I,I},{I,I},{我,我}},Z},
                    {{{I,I},{I,I},{我,我}},Z},
                    {{{I,I},{I,I},{我,我}},Z}
                  };

由于以Z 是一台为一个整数,我们并不需要打破它的任何进一步。只需更换 I 以Z s的实际整数值,就大功告成了:

  MYSTRUCT富[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}
                  };

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};
}

解决方案

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}
                  };

这篇关于如何初始化结构的C中的数组成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆