简体   繁体   中英

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 . 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).

My question is this:

How to declare a character pointer that points to the member variable c using abc that was declared above ?

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

&(*abc)->c

Note the paranthesis around *abc . 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 . .

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!

       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:

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

You have an ABC object, and you want a pointer to one of the char members within that object.

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

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.

No, you don't.

How to declare a character pointer that points to the member variable c using abc that was declared above ?

This way:

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

it declares p as a pointer to char and p points to structure member a . The parentheses are needed as postfix operators have higher precedence than unary operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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