c语言结构体嵌套

转载自:https://blog.csdn.net/zhudong10/article/details/49496221

C语言中结构体是一种构造类型,和数组、基本数据类型一样,可以定义指向该种类型的指针。结构体指针的定义类似其他基本数据类型的定义,格式如下

struct 结构体名 * 指针名;

比如:

struct person{char[20] name; int age;};//先定义一个人的结构体

struct person *p;//然后可以定义一个人的结构体指针

struct person p1 = {"zhangsan",20};

*p = &p1;//结构体指针的初始化

当定义结构体时,如果结构体中的成员又是一个结构体,那么就称为结构体的嵌套。比如:

struct room{

   int chair;

   int computer;

   struct person children;

};

嵌套的结构体初始化方式如下:

struct room r1 = {1,1,{"xiaohong",7}};

嵌套结构体的初始化参照基本结构体的初始化方式,对结构体的元素分别进行初始化。

结构体中不可以嵌套自身的结构体,但是可以嵌套指向自身的指针。

关于上面所述的结构体嵌套及嵌套指向自身的结构体指针,下面有几个实例:

 
  1. 结构体的嵌套以及结构体指针

  2. #include "stdafx.h"

  3. #include <string.h>

  4.  
  5. int main(int argc, char* argv[])

  6. {

  7. //结构体指针

  8. struct office{

  9. int chair;

  10. int computer;

  11. } ;

  12. struct office officeOne = {10,10};

  13. struct office *p = &officeOne;

  14. printf("chair = %d,computer = %d\n",(*p).chair,(*p).computer);

  15. return 0;

  16. }

 

  1. #include "stdafx.h"

  2. #include <string.h>

  3. //结构体指针以及结构体嵌套

  4. struct employee{

  5. char name[10];

  6. int age;

  7. };

  8. int main(int argc, char* argv[])

  9. {

  10. //结构体嵌套

  11. struct office{

  12. int chair;

  13. int computer;

  14. struct employee em;

  15. } ;

  16. struct office officeOne = {10,10,{"zhangsan",25}};

  17. struct office *p = &officeOne;

  18. printf("chair = %d,computer = %d\nname = %s,age = %d\n",(*p).chair,(*p).computer,officeOne.em.name,officeOne.em.age);

  19. return 0;

  20. }

 

  1. #include "stdafx.h"

  2. #include <string.h>

  3. //结构体指针以及结构体嵌套结构体指针

  4. int main(int argc, char* argv[])

  5. {

  6. //结构体指针

  7. struct office{

  8. int chair;

  9. int computer;

  10. struct office *of1;

  11. } ;

  12. struct office officeOne = {10,10,NULL};

  13. struct office officeTwo = {10,10,&officeOne};

  14. printf("chair = %d,computer = %d\nchair1 = %d,computer1 = %d\n",officeTwo.chair,officeTwo.computer,(*(officeTwo.of1)).chair,(*(officeTwo.of1)).computer);

  15. return 0;

  16. }

 

 

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中的结构体可以包含其他结构体作为其成员,这种结构体嵌套结构体的形式能够更灵活地组织和管理数据。对于结构体嵌套结构体,可以使用指针来访问和操作。 首先,我们需要定义两个结构体,一个作为外层结构体,另一个作为内层结构体。接着,我们可以在外层结构体中声明内层结构体的指针作为成员。这样,外层结构体的成员就是一个指向内层结构体的指针。 为了方便理解,下面我们举一个例子来说明结构体嵌套结构体指针的用法。 ```c #include <stdio.h> // 内层结构体 typedef struct { int x; int y; } Point; // 外层结构体 typedef struct { int id; Point* point; } Shape; int main() { Point p1 = {1, 2}; Point p2 = {3, 4}; Shape s1 = {1, &p1}; Shape s2 = {2, &p2}; printf("s1.id = %d\n", s1.id); printf("s1.point->x = %d\n", s1.point->x); printf("s1.point->y = %d\n", s1.point->y); printf("s2.id = %d\n", s2.id); printf("s2.point->x = %d\n", s2.point->x); printf("s2.point->y = %d\n", s2.point->y); return 0; } ``` 在上面的例子中,我们定义了一个内层结构体`Point`表示二维坐标点,另外定义了一个外层结构体`Shape`表示图形,其中成员`point`是指向`Point`结构体的指针。 在`main`函数中,我们分别创建两个内层结构体对象`p1`和`p2`,并设置其属性。 然后,我们创建两个外层结构体对象`s1`和`s2`,并将内层结构体对象的指针分别赋值给它们的`point`成员。 最后,我们通过外层结构体对象访问内层结构体对象的属性,通过指针的方式实现了结构体嵌套结构体的访问。 运行程序后,输出结果如下: ``` s1.id = 1 s1.point->x = 1 s1.point->y = 2 s2.id = 2 s2.point->x = 3 s2.point->y = 4 ``` 结构体嵌套结构体指针能够帮助我们更有效地组织和操作数据,提高代码的可读性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值