Accessing variables from a struct

Zeeshan Rang picture Zeeshan Rang · Sep 3, 2009 · Viewed 8.6k times · Source

How can we access variables of a structure? I have a struct:

typedef struct {
   unsigned short a;
   unsigned shout b;
} Display;

and in my other class I have a method:

int NewMethod(Display **display)
{
   Display *disp=new Display();
   *display = disp;
   disp->a=11;
}

What does **display mean? To access variables of struct I have used ->, are there other methods too?

Answer

unwind picture unwind · Sep 3, 2009

As Taylor said, the double asterisk is "pointer to pointer", you can have as many levels of pointers as you need.

As I'm sure you know, the arrow operator (a->b) is a shortcut for the asterisk that dereferences a pointer, and the dot that accesses a field, i.e.

a->b = (*a).b;

The parentheses are necessary since the dot binds tighter. There is no such operator for double asterisks, you have to first de-reference to get to the required level, before accessing the fields:

Display **dpl = ...;

(*dpl)->a = 42;

or

(**dpl).a = 42;