Pointers to structures

There are two ways to declare a pointer to a structure. If the typeName or the structName have already been defined:

Syntax

typeName *pointerName;
// or
struct structName *pointerName;

Code example

// structure type to handle color values
typedef struct {
    int red;
    int green;
    int blue;
} rgb;
...
rgb *pColor;

Code example

// structure type to handle color values
struct rgb {
    int red;
    int green;
    int blue;
}
...
struct rgb *pColor;

Accessing structure members using a pointer

If a pointerName has already been defined:

Syntax

pointerName -> memberName;

First, the pointer must be initialized to point to the address of the structure itself: pointerName = &structVariable;

Code example

// structure type to handle color values
typedef struct {
    int red;
    int green;
    int blue;
} rgb;
...
rgb color;   // rgb variable
rgb *pColor; // pointer to color

int  main (void) {
    pColor        = color;
    pColor->red   = 0;     // Set   red using pColor
    pColor->green = 145;   // Set green using pColor
    pColor->blue  = 189;   // Set  blue using pColor
}

We will later see in an example how CMSIS uses this to access core peripherals of a Cortex-M based device.