简体   繁体   English

访问嵌套结构中的成员

[英]Accessing a member in a nested structure

有没有一种方法可以访问嵌套在其他两个结构中的结构的各个成员,而无需多次使用点运算符?

Is there a way to access individual members of a structure that is nested inside two other structures without using the dot operator multiple times? 有没有一种方法可以访问嵌套在其他两个结构中的结构的各个成员,而无需多次使用点运算符?

No. Not via standard C. 否。不通过标准C。

To make the accessing code cleaner however, you might consider some static inline helper functions. 但是,为了使访问代码更整洁,您可以考虑使用一些static inline帮助函数。

For example: 例如:

struct snap {
    int memb;
};

struct bar {
    struct snap sn;
};

struct foo {
    struct bar b;
}

static inline int foo_get_memb(const struct foo *f)
{
    return f->b.sn.memb;
}

Unlike some variants of BASIC or Pascal that have a with keyword which allows you to access inner members of a structure directly, C has no such construct. 与某些with关键字的BASIC或Pascal变体不同,它允许您直接访问结构的内部成员,而C没有这种构造。

You can do this however with pointers. 您可以使用指针执行此操作。 If you have a particular inner member you're going to access frequently, you can store the address of that member in a pointer and access the member through the pointer. 如果您有特定的内部成员,那么您将经常访问该成员,则可以将该成员的地址存储在指针中,并通过指针访问该成员。

Suppose for example you had the following data structures: 例如,假设您具有以下数据结构:

struct inner2 {
    int a;
    char b;
    float c;
};

struct inner1 {
    struct inner2 in2;
    int flag;
};

struct outer {
    struct inner1 in1;
    char *name;
};

And a variable of the outer type: 和外部类型的变量:

struct outer out;

Instead of accessing the members of the innermost struct like this: 而不是像这样访问最里面的struct的成员:

out.in1.in2.a = 1;
out.in1.in2.b = 'x';
out.in1.in2.c = 3.14;

You declare a pointer of type struct inner2 and give it the address of out.in1.in2 . 您声明类型为struct inner2的指针,并将其地址指定为out.in1.in2 Then you can work directly with that. 然后,您可以直接使用它。

struct inner2 *in2ptr = &out.in1.in2;
in2ptr->a = 1;
in2ptr->b = 'x';
in2ptr->c = 3.14;

You could use the -> operator. 您可以使用->运算符。

You could take the address of an inner member and then access it via the pointer. 您可以获取内部成员的地址,然后通过指针进行访问。

Not completely answering your question. 没有完全回答您的问题。

The 1st member of any struct can be accessed by taking the struct 's address, casting it to a pointer type pointing to the struct 's 1st member and dereferencing it. 可以通过获取struct的地址,将其转换为指向该struct的第一个成员的指针类型并对其取消引用来访问任何struct的第一个成员。

struct Foo
{
  int i;
  ...
};

struct Foo foo = {1};
int i = *((int*) &foo); /* Sets i to 1. */

Adapting this to nested struct's gives us for example: 例如,将其调整为嵌套结构即可:

struct Foo0
{
  struct Foo foo;
  ...
};

struct Foo1
{
  struct Foo0 foo0;
  ...
};

struct Foo2
{
  struct Foo1 foo1;
  ...
};

struct Foo2 foo2;
foo2.foo1.foo0.foo.i = 42;
int i = *((int*) &foo2); /* Initialises i to 42. */

struct Foo0 foo0 = {*((struct Foo*) &foo2)}; /* Initialises foo0 to f002.f001.foo0. */

This is well defined, as the C-Standard guarantees that there is no padding before a struct's 1st member. 这是定义明确的,因为C-Standard保证在结构的第一个成员之前没有填充。 Still it's not nice. 仍然不是很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM