如何使用另一个struct中的struct?

编程入门 行业动态 更新时间:2024-10-13 08:24:34
本文介绍了如何使用另一个struct中的struct?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用嵌套结构,但我不知道如何在其中输入数据,例如:

struct A { int data; struct B; }; struct B { int number; };

所以在主要使用它时:

int main(){ A阶段; stage.B.number; }

是不是如果不是如何使用它

解决方案

结构的每个成员变量通常具有名称和类型。在您的代码中, A 的第一个成员具有类型 int 和名称 data 。第二个成员只有一个类型。你需要给它一个名字。让我们说 b :

struct A { int data ; B b; };为了做到这一点,编译器需要已经知道 B 是,因此在之前声明 A

<要访问嵌套成员,请按名称引用每个成员,并以分隔。:

一个阶段; stage.b.number = 5;

I want to use a nested structure but i dont know how to enter data in it, for example:

struct A { int data; struct B; }; struct B { int number; };

So in the main when I come to use it :

int main() { A stage; stage.B.number; }

Is that right if not how do i Use it??

解决方案

Each member variable of a struct generally has a name and a type. In your code, the first member of A has type int and name data. The second member only has a type. You need to give it a name. Let's say b:

struct A { int data; B b; };

To do that, the compiler needs to already know what B is, so declare that struct before you declare A.

To access a nested member, refer to each member along the path by name, separated by .:

A stage; stage.b.number = 5;

更多推荐

如何使用另一个struct中的struct?

本文发布于:2023-10-08 14:52:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472949.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   struct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!