一个结构数组初始化的内部结构数组 [英] Initializing structure array inside a structure array

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

问题描述

我在初始化结构数组里面我的结构数组的问题。例如,如果我有一个code如下:

I have a question in initializing my structure array inside a structure array. for example if I have a code as below:

#include <stdio.h>

int main() {

typedef struct {
    int a;
    int b;
    int c;
} FIRST_T;

typedef struct {
    int x;
    int y;
    int z;
    FIRST_T *p;
} SECOND_T;

FIRST_T p1[]={{1,2,3},{3,4,5},{6,7,8}};
FIRST_T p2[]={{4,5,6},{7,8,9}};

SECOND_T my_second[]=
{
{1,2,3,p1},
{4,5,6,p2}
};

}

如果我必须初始化我在第二个数组intialization部分本身第一个数组,那么我将如何写我SECOND_T的类型定义?

if I have to initialize my first array in second array intialization part itself, then how would I write my typedef of SECOND_T? like

SECOND_T my_second[]=
{
{1,2,3,{{1,2,3},{4,5,6},{7,8,9}}},
{4,5,6,{{1,1,1},{2,2,2}}}
};

那怎么是我的SECOND_T?

then how should be my SECOND_T?

我相信,我不能把它定义为:

I am sure I can't define it as:

typedef struct {
    int x;
    int y;
    int z;
    FIRST_T p[]; //(I know its a blunder)
} SECOND_T;

请帮忙。

推荐答案

您不能定义在C无限阵列类型,你必须指定尺寸。所以,你要么做的:

You can't define a type with unbounded array in C, you have to specify the dimension. So you either do:

typedef strut {
    int x;
    int y;
    int z;
    FIRST_T f[SOME_VALUE];
} SECOND_T;

然后初始化 SOME_VALUE 成员计数总是,或做你做的方式。

and then initialize SOME_VALUE count of members always, or do it the way you did.

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

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