Access the address of a struct element using a pointer to a pointer

Suppose 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 the pointer pointing to the structure pointer *abc and *abc is the 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 (given that pointers are 4 bytes in size and characters are 1 byte in size).

My question is:

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

+4
source share
6 answers
 sizeof(**abc) will be 4, sizeof(*abc) will also be 4 and sizeof(abc) will be 3 

I think it should be, apart from filling out 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 a member variable c do

 &(*abc)->c 

Note the brackets around *abc . The reason for this is that -> has a higher priority than * , and therefore you need to make sure that the first dereference (moving from pointer to pointer to pointer) occurs first.

Or you can do

 &(**abc).c 

For the same reason, in parentheses ... you need to make sure that you dereference (twice) before applying the select-through-object-name element . .

+5
source

If all of your pointers are valid so that you can dereference them, you can do this as follows:

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

Do you mean?

 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 realized that I don’t even understand the thought process that led to this issue. Let me break it:

  • Q: from a link to a structure, how do I get a link to a member?

    • A: using an item access operator .

       char &c = rc; 
  • Q: from a link to a structure, how do I get a pointer to a member?

    • A: using the address operator &

       char *c = &r.c; 
  • Q: from the structure pointer, how do I get a member link?

    • A: using element access operator ->

       char &c = p->c; 
  • Q: from a structure pointer, how do I get a member pointer?

    • A: if there was only a way to combine 2 and 3!

       char *c = &p->c; 
  • Q: from pointer to pointer, how do I get a regular pointer?

    • A: using the dereference operator *

       ABC **pp = foo(); ABC *p = *pp; 
  • 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

May I ask what step in this process caused difficulties?

+3
source

&(*abc)->c , but you can go in steps for legibility:

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

There are several ways to interpret your question due to the slightly strange nomenclature used inside it. I will study two here.


Scenario 1

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

Well, here is the object:

 ABC obj; 

Here are the pointers you mentioned so far:

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

And here the pointer you are requesting is declared in three equivalent ways:

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

Scenario 2

You do not have an ABC object, but you want a member-to-member pointer that can later be applied to member c some ABC object that you later receive.

No, you do not.

+3
source

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

In this way:

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

it declares p as a pointer to char and p points to a member of a . Brackets are needed because postfix operators have higher priority than unary operators.

+2
source

Source: https://habr.com/ru/post/1483604/


All Articles