我想使用嵌套结构,但我不知道如何在其中输入数据,例如:
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?
发布评论