简体   繁体   English

通过指向指针的方法访问struct元素的地址

[英]Accessing address of struct element via pointer-to-pointer

Lets say I have a structure : 可以说我有一个结构:

struct ABC 
{
   char a;
   char b;
   char c;
}

I can declare a pointer to a pointer to the above structure as : 我可以声明指向上述结构的指针:

struct ABC** abc

Now abc is a pointer pointing to the structure pointer *abc and *abc is a structure pointer pointing to the structure abc . 现在abc是指向结构指针*abc指针,而*abc是指向结构abc的结构指针。 Thus; 从而; sizeof(**abc) will be 4, sizeof(*abc) will also be 4 and sizeof(abc) will be 3 (considering pointers are 4 bytes in size and characters are 1 byte in size). sizeof(**abc)将为4, sizeof(*abc)也将为4, sizeof(abc)将为3(考虑指针大小为4个字节,字符大小为1个字节)。

My question is this: 我的问题是:

How to declare a character pointer that points to the member variable c using abc that was declared above ? 如何使用上面声明的abc声明一个指向成员变量c的字符指针?

sizeof(**abc) will be 4, sizeof(*abc) will also be 4 and sizeof(abc) will be 3 

I think that should be, assuming no padding of the structure, 我认为应该是,假设没有填充结构,

sizeof(**abc) will be 3, sizeof(*abc) will also be 4 and sizeof(abc) will be 4
                     ^^^                                                    ^^^  
                      Change here                                           change here

To get a pointer to member variable c do 要获得指向成员变量c的指针

&(*abc)->c

Note the paranthesis around *abc . 请注意*abc周围的paranthesis。 The reason for this is that -> has a higher precedence than * and so you need to make sure the first dereference (to go from pointer-to-pointer to pointer) happens first. 原因是->具有比*更高的优先级,因此您需要确保首先发生第一个取消引用(从指针指向指针)。

Or you can do 或者你可以做到

&(**abc).c

Same reason for the parenthesis... need to make sure you've dereferenced (twice) before applying the member-selection-via-object-name . 括号的相同原因......在应用member-selection-via-object-name之前需要确保已取消引用(两次) . .

Provided all of your pointers are valid, so that you can dereference them, you can do it like this: 如果你的所有指针都是有效的,那么你可以取消引用它们,你可以这样做:

char* a_ptr = &((*abc)->a);
or 
char* b_ptr = &((**abc).b);

You mean like? 你的意思是?

char *c = &((**abc).c);
char *d = &((*abc)->c);

Or you could write something legible: 或者你可以写一些清晰的东西:

ABC **pp == foo();
ABC *p = *pp;
char *c = &(p->c);

Since writing this, I realised I don't even understand the thought process that led to this question. 自写这篇文章以来,我意识到我甚至不理解导致这个问题的思维过程。 Let's break it down: 让我们分解一下:

  1. Q: from a structure reference, how do I get a member reference? 问:从结构参考中,我如何获得成员参考?

    • A: using the member access operator . 答:使用成员访问运算符.

       char &c = rc; 
  2. Q: from a structure reference, how do I get a pointer to a member? 问:从结构引用中,如何获得指向成员的指针?

    • A: using the address operator & 答:使用地址运算符&

       char *c = &r.c; 
  3. Q: from a structure pointer, how do I get a member reference? 问:从结构指针,我如何获得成员参考?

    • A: using the member access operator -> 答:使用成员访问操作符->

       char &c = p->c; 
  4. Q: from a structure pointer, how do I get a pointer to a member? 问:从结构指针,我如何获得指向成员的指针?

    • A: if only there were some way to combine 2 and 3! - 答:如果只有一些方法来结合2和3!

       char *c = &p->c; 
  5. Q: from a pointer to a pointer, how do I get a regular pointer? 问:从指针到指针,如何获得常规指针?

    • A: using the dereference operator * 答:使用解除引用运算符*

       ABC **pp = foo(); ABC *p = *pp; 
  6. Q: from a pointer to a pointer to a structure, how do I get a pointer to a member? 问:从指向结构的指针,如何获得指向成员的指针?

    • A: Left as an exercise for the reader 答:留给读者练习

Can I ask which step in this process caused the difficulty? 我可以问这个过程中哪一步造成了困难吗?

&(*abc)->c , but you might want to go in steps for legibility's sake: &(*abc)->c ,但为了清晰起见,您可能需要采取措施:

struct ABC *foo = *abc;
char *bar = &foo->c;

There are several ways to interpret your question, due to some slightly strange nomenclature in use within it. 有几种方法可以解释您的问题,因为它内部使用了一些略微奇怪的命名法。 I'll explore two here. 我会在这里探讨两个。


Scenario 1 场景1

You have an ABC object, and you want a pointer to one of the char members within that object. 你有一个ABC对象,你想要一个指向该对象中的一个char成员的指针。

Well, here's the object: 嗯,这是对象:

ABC obj;

Here are the pointers you've mentioned so far: 这是你到目前为止提到的指针:

ABC*  ptr1 = &obj;
ABC** ptr2 = &ptr1;

And here's the pointer you're asking for, declared in three equivalent ways: 这是你要求的指针,以三种等价方式声明:

char* theChar1 = &(obj.c);
char* theChar2 = &(ptr1->c);
char* theChar3 = &((*ptr2)->c);

Scenario 2 情景2

You don't have an ABC object, but you want a pointer-to-member that can later be applied to the member c of some ABC object that you obtain later on. 您没有ABC对象,但是您需要一个指向成员的指针,以后可以将其应用于稍后获得的某些ABC对象的成员c

No, you don't. 不,你没有。

How to declare a character pointer that points to the member variable c using abc that was declared above ? 如何使用上面声明的abc声明一个指向成员变量c的字符指针?

This way: 这条路:

char *p = &(*abc)->a;

it declares p as a pointer to char and p points to structure member a . 它将p声明为指向char的指针, p指向结构成员a The parentheses are needed as postfix operators have higher precedence than unary operators. 需要括号,因为后缀运算符的优先级高于一元运算符。

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

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