C Pointers to Structures

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Pointers to Structures?

A pointer to a struct is common because it avoids copying and lets functions modify the original. Use the arrow operator ptr->member, which is shorthand for (*ptr).member. Pointers to structures also power linked lists, trees, and dynamically allocated records.

Remember
struct Point pt = {1, 2};
struct Point *p = &pt;
p->x = 5;           /* same as (*p).x = 5 */
struct Point *q = malloc(sizeof *q);
q->y = 9;

Cheat Sheet

Keep these facts handy while you work with Pointers to Structures in C.

TopicDetail
Arrowp->member
Equivalent(*p).member
Allocatemalloc(sizeof *p)
Pass to functionAvoids copying the struct

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Structures and Functions.

Example 1 — Move a point through a pointer

The function edits the caller's struct.

C
#include <stdio.h>

struct Point { int x, y; };

static void move(struct Point *p, int dx, int dy) {
    p->x += dx;
    p->y += dy;
}

int main(void) {
    struct Point pt = {1, 2};
    struct Point *p = &pt;

    p->x = 5;
    printf("(%d, %d)\n", pt.x, (*p).y);
    move(&pt, 2, 3);
    printf("(%d, %d)\n", pt.x, pt.y);
    return 0;
}

How It Works

  1. p->x = 5 changes pt.x.
  2. (*p).y is the long form of p->y.
  3. move receives the address, so changes persist in main.

Edge Cases & Pitfalls

Check these before relying on Pointers to Structures in real programs.

dot

Dot on a pointer

p.x fails when p is a pointer. Use p->x.

null

NULL struct pointers

Check p != NULL before p->member.

Key Takeaways

  • Arrow: pointer member access.
  • Efficient: pass pointers, not copies.
  • Dynamic: allocate structs on the heap.

One line: A pointer to a struct is common because it avoids copying and lets functions modify the original.

Frequently Asked Questions

The arrow operator accesses a member through a pointer to a structure: ptr->field is the same as (*ptr).field.
Use . when you have a struct object and -> when you have a pointer to a struct.
Continue with "C Structures and Functions" next. Pass structs by value or pointer and return them from functions. The C Structures, Unions and Enums hub lists every topic in order.

Next: Structures and Functions

Pass structs by value or pointer and return them from functions.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful