c++中结构体中存储结构体数组

c++中结构体中存储结构体数组

  1. 首先定义存结构体数组的结构体和子结构体
#ifndef ZYXX_H
#define ZYXX_H

struct child
{
    char a1[10];
    int a2;
};
struct Parent{
    child *c1;
};
#endif // ZYXX_H

  1. 然后首先创建子结构体数组,分别赋值,最后把子结构体数组存到父结构体中;
#include <iostream>
#include"zyxx.h"
#include"string.h"
using namespace std;

int main()
{
    child c1[3];
    strcpy(c1[0].a1,"test1");
    c1[0].a2=1;
    strcpy(c1[1].a1,"test2");
    c1[1].a2=2;
    strcpy(c1[2].a1,"test3");
    c1[2].a2=3;
    Parent P1;
    P1.c1=c1;

    cout<<P1.c1[1].a2<<endl;
    return 0;
}
发布了13 篇原创文章 · 获赞 25 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43842143/article/details/103532721