5

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 ?

15
  • 11
    The title is straight out of "David Lynch's C++", starring Harry Dean Stanton as The New Operator. Commented May 30, 2013 at 14:55
  • 9
    Oh come on.. fix your title! Commented May 30, 2013 at 14:55
  • 1
    Thats a heck of a "subject" there! How about simply, "Declaring Pointers to structure members?" Or any summary?? Commented May 30, 2013 at 14:56
  • 1
    @abelenky: That would be a worse title. Naming language features in a list is not useful! Commented May 30, 2013 at 14:56
  • 2
    Come on guys that's an attention grabbing and quite humorous title :) Commented May 30, 2013 at 14:56

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

3

&(*abc)->c, but you might want to go in steps for legibility's sake:

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

Comments

3

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.

Comments

3

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 = r.c;
      
  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?

2 Comments

Very sorry sir, but I could not go thru the steps myslef correctly. I was getting a lot of errors. Sorry I had to ask the question.
No need to apologise! It might have helped, though, if you showed what you tried and what the errors were. Without that, we were trying to guess what you might have got wrong.
2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.