What are Pointers to Structures in C- Free C Programming Course

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

PREV_LEC  |  NEXT_LEC

Free C Programming Course · Chapter 10 · Lecture 13

Pointers to Structures in C

Master pointers to structures in C: the address operator, the arrow operator, precedence traps and pointer stepping, with tested examples for embedded C programming.

Topics in This Free Embedded C Course Lecture

pointers to structures in C arrow operator structure pointer operator precedence free C tutorial C programming for embedded systems

A pointer holds an address, and pointers to structures in C hold the address of a whole structure variable. They are everywhere in embedded firmware: a driver receives a pointer to its configuration, an interrupt handler walks a pointer through a table of descriptors, and a peripheral register block is accessed through a pointer to a structure. In this lecture of our free C programming course you will declare a structure pointer, use the arrow operator, understand why the parentheses in (*p).member are essential, and watch a pointer step across an array of structures.

What You Will Learn

  • How to declare and initialise a pointer to a structure
  • Two ways to reach members: (*p).member and p->member
  • Why dot has higher precedence than the dereference operator
  • How to make a pointer to a single member
  • How pointer arithmetic moves in whole-structure steps

Prerequisites

You need basic pointers (the & and * operators), structure definitions and the dot operator. If any of those are shaky, revisit the pointers chapter and the first lectures of this chapter.

Declaring Pointers to Structures in C

A structure pointer is declared like any other pointer: the type is the structure type, followed by a star. It can then be set to the address of an existing structure variable with the address-of operator.

struct motor {
    char name[12];
    int  rpm;
    int  max_rpm;
};

struct motor m = { "pump", 1200, 3000 };
struct motor *p = &m;       /* p holds the starting address of m */

p points to the first byte of m

1

m

The structure variable: name, rpm and max_rpm stored together, 20 bytes.

2

&m

The address of its first byte, which is also the address of the first member.

3

p

A separate pointer variable that stores that address.

Accessing Members Through a Structure Pointer

Dereferencing p gives the structure itself, so *p means the same as m. That suggests the first way to reach a member: (*p).rpm. The parentheses are required, and the next section explains why. Because that syntax is awkward, C provides the arrow operator, written as a hyphen followed by a greater-than sign: p->rpm. Both forms mean the same thing.

FormReads asWhen to use
m.rpmmember of a structure variableYou have the structure itself
(*p).rpmmember of the structure p points toRarely; shows what the arrow expands to
p->rpmmember of the structure p points toAlways preferred with pointers

Complete Example: Pointers to Structures in C

Save as p1_struct_ptr.c. It reads through the pointer, writes through the pointer and also makes a pointer to a single member.

#include <stdio.h>

struct motor {
    char name[12];
    int  rpm;
    int  max_rpm;
};

int main(void)
{
    struct motor m = { "pump", 1200, 3000 };
    struct motor *p = &m;

    printf("m.rpm        = %d\n", m.rpm);
    printf("(*p).rpm     = %d\n", (*p).rpm);
    printf("p->rpm       = %d\n", p->rpm);

    p->rpm += 300;
    printf("after p->rpm += 300, m.rpm = %d\n", m.rpm);

    int *rp = &m.rpm;
    *rp = 0;
    printf("after *rp = 0, p->rpm = %d\n", p->rpm);

    printf("address of m      : %p\n", (void *)&m);
    printf("address of m.name : %p\n", (void *)&m.name);
    printf("address of m.rpm  : %p\n", (void *)&m.rpm);
    printf("offset of rpm = %td bytes\n", (char *)&m.rpm - (char *)&m);
    return 0;
}

Build and Run

$ gcc -Wall -Wextra -std=c11 p1_struct_ptr.c -o p1
$ ./p1
m.rpm        = 1200
(*p).rpm     = 1200
p->rpm       = 1200
after p->rpm += 300, m.rpm = 1500
after *rp = 0, p->rpm = 0
address of m      : 0x7fffd0da8510
address of m.name : 0x7fffd0da8510
address of m.rpm  : 0x7fffd0da851c
offset of rpm = 12 bytes

The three addresses will differ on your machine, but the pattern will not: the address of the structure equals the address of its first member, and rpm sits 12 bytes later, right after the 12-byte name array. Writing through p changed m, because p refers to the very same bytes, not a copy.

Why (*p).rpm Needs Parentheses

The dot operator has higher precedence than the dereference star. Without parentheses, *p.rpm is read as *(p.rpm), which tries to apply the dot to a pointer. The compiler stops you, and modern GCC even suggests the fix:

$ gcc -std=c11 n7_precedence_bad.c -o n7
n7_precedence_bad.c:10:22: error: 'p' is a pointer; did you mean to use '->'?
   10 |     printf("%d\n", *p.rpm);   /* ERROR: parsed as *(p.rpm) */
      |                      ^
      |                      ->

The arrow operator has the same precedence as the dot and also groups from left to right, so p->rpm needs no parentheses and also chains cleanly, for example cfg->frame.parity when frame is a nested structure.

Pointers to Individual Members

You can also point at one member of a structure variable, as rp did above. The expression &m.rpm means &(m.rpm), since dot binds tighter than address-of. The pointer’s type follows the member, so it is an int * here, not a struct motor *.

Pointer Arithmetic Moves by Whole Structures

When a pointer to a structure is incremented, it advances by sizeof the whole structure, not by one byte. That makes it perfect for walking an array of structures. Save as p3_array_step.c:

#include <stdio.h>

struct motor {
    char name[12];
    int  rpm;
    int  max_rpm;
};

int main(void)
{
    struct motor fleet[3] = {
        { "pump",  1200, 3000 },
        { "fan",    800, 1500 },
        { "belt",   450,  900 }
    };

    struct motor *p = fleet;
    printf("sizeof(struct motor) = %zu\n", sizeof(struct motor));

    for (int i = 0; i < 3; i++, p++)
        printf("%s: rpm=%d  address step from start = %td bytes\n",
               p->name, p->rpm, (char *)p - (char *)fleet);
    return 0;
}
$ ./p3
sizeof(struct motor) = 20
pump: rpm=1200  address step from start = 0 bytes
fan: rpm=800  address step from start = 20 bytes
belt: rpm=450  address step from start = 40 bytes

p++ jumps 20 bytes, one whole struct motor

Iterationp points toOffset from fleet
0fleet[0] (pump)0 bytes
1fleet[1] (fan)20 bytes
2fleet[2] (belt)40 bytes

Real-World Use of Pointers to Structures in C

  • Driver contexts. A function receives struct uart_cfg *cfg and reads settings without copying them.
  • Descriptor tables. DMA and interrupt tables are arrays of structures walked by a pointer.
  • Register blocks. On real hardware a pointer to a register structure is set to the peripheral’s base address, and regs->ctrl reaches the control register. In our lab we use an ordinary variable instead of a real address so that it runs on a PC.

Common Mistakes and Troubleshooting

  • Using the dot on a pointer. p.rpm is an error; use p->rpm.
  • Using the arrow on a structure variable. m->rpm is also an error.
  • Forgetting parentheses. *p.rpm does not do what you think.
  • Using an uninitialised pointer. A structure pointer that never received an address will crash the program when dereferenced. Set it to &m or NULL at declaration.
  • Printing pointers with the wrong format. Cast to void * and use %p.

Best Practices, Performance and Safety

Guidelines

  • Check pointers against NULL before dereferencing, especially when they come from outside a function.
  • Pass large structures by pointer to avoid copying many bytes onto a small microcontroller stack.
  • Use const struct motor * when a function must only read the structure.
  • Do not return the address of a local structure; it disappears when the function returns.

Summary and Key Takeaways

  • A pointer to a structure stores the address of the whole structure, which equals the address of its first member.
  • Use p->member, which is shorthand for (*p).member.
  • Dot has higher precedence than star, so the parentheses are not optional in the long form.
  • Incrementing a structure pointer advances by sizeof(struct).
  • A pointer to a member has the member’s type, not the structure’s.

Conclusion

Pointers to structures in C are the bridge between data layout and efficient code. With the arrow operator and an understanding of precedence, you can write functions that read and update structures in place. Next, we put a pointer inside a structure and see how the same operators combine.

Suggested Images for This Lecture

Image ideaALT text
Arrow from a pointer box to a structure boxPointers to structures in C showing a structure pointer holding the address of a structure
Three structures in a row with a moving arrowPointer stepping across an array of structures in a free embedded C course
Dot versus arrow comparison cardArrow operator versus dot operator for pointers to structures in C

Frequently Asked Questions

What is a pointer to a structure in C?

It is a pointer variable whose type is a structure type and which holds the address of a structure variable.

What is the difference between dot and arrow?

Use dot with a structure variable and arrow with a pointer to a structure. p->x is shorthand for (*p).x.

Why do I need parentheses in (*p).member?

Dot binds tighter than the dereference star, so without parentheses the compiler reads *(p.member).

Does p++ move by one byte?

No. It moves by the size of the structure the pointer points to.

Can I point to just one member of a structure?

Yes, with &m.member. The pointer has the type of that member.

Why use structure pointers in embedded C programming?

They avoid copying data, let functions modify the caller’s structure and map naturally onto peripheral register blocks.

Next in the Free Embedded C Course

Learn how a pointer stored inside a structure behaves and which operators to use.

PREV_LEC  |  NEXT_LEC

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →