Skip to content
StudyFame

Structures and Pointers in C

When a pointer holds the address of a structure, how do you reach a member like roll? You could write (*p).roll, but C gives a cleaner way: the arrow operator p->roll.

In this lesson you will use pointers to structures, learn exactly when to use . and when to use ->, walk through an array of records with a pointer, and meet self-referential structures, the building block of linked lists.

What You Will Learn

  • How to declare a pointer to a structure.
  • Why (*p).member needs brackets.
  • How the arrow operator -> works.
  • Dot vs arrow: which one to use.
  • How to move through an array of structures with a pointer.
  • What a self-referential structure is.

Structure Pointer and the Arrow Operator Explained

Exam definition: A structure pointer is a pointer that stores the address of a structure variable. Its members are accessed with the arrow operator (->): p->member, which means exactly the same as (*p).member.

Memory trick: Dot for the thing, arrow for the address. If you are holding the structure itself, use .. If you are holding a pointer (an arrow pointing to it), use ->. The arrow even looks like it's pointing!

Syntax of a Structure Pointer

struct student s = {101, "Asha", 88};
struct student *p = &s;      /* p points to s */

s.roll        /* structure variable  -> dot   */
p->roll       /* pointer             -> arrow */
(*p).roll     /* same as p->roll (brackets needed!) */

Why the brackets in (*p).roll?

The dot has higher precedence than *. Without brackets, *p.roll means *(p.roll), which is an error because p is a pointer, not a structure. The arrow avoids this confusion: p->roll is short for (*p).roll.

Example 1: Dot, Arrow and (*p) Side by Side

#include <stdio.h>
#include <string.h>

struct student
{
    int roll;
    char name[20];
    int marks;
};

int main(void)
{
    struct student s = {101, "Asha", 88};
    struct student *p = &s;

    printf("Using s.     : %d %s %d\n", s.roll, s.name, s.marks);
    printf("Using (*p).  : %d %s %d\n", (*p).roll, (*p).name, (*p).marks);
    printf("Using p->    : %d %s %d\n", p->roll, p->name, p->marks);

    p->marks += 5;                   /* change s through the pointer */
    strcpy(p->name, "Asha V.");
    printf("After update : %d %s %d\n", s.roll, s.name, s.marks);

    printf("sizeof(s) = %zu, sizeof(p) = %zu\n", sizeof(s), sizeof(p));

    return 0;
}

Output

Using s.     : 101 Asha 88
Using (*p).  : 101 Asha 88
Using p->    : 101 Asha 88
After update : 101 Asha V. 93
sizeof(s) = 28, sizeof(p) = 8

Code Explanation

  1. struct student *p = &s; stores the address of s in p.
  2. All three lines print the same data: s.roll, (*p).roll and p->roll are the same member.
  3. p->marks += 5; changes s.marks, because p points to s.
  4. sizeof(s) is the whole record (4 + 20 + 4 = 28 bytes here); sizeof(p) is just an address (8 bytes on 64-bit). Passing p is much cheaper than copying s.

Practice: accept student information in a structure and display it using a pointer.

How a Pointer Moves Through an Array of Structures

Pointer arithmetic works with structures too. p + 1 jumps by sizeof(struct student) bytes: to the next whole record.

struct student s[3];   struct student *p = s;

  s[0]            s[1]            s[2]
+---------------+---------------+---------------+
| 101|Asha|88   | 102|Ravi|72   | 103|Neha|95   |
+---------------+---------------+---------------+
  ^ p             ^ p+1           ^ p+2
  p->name = "Asha"   (p+1)->name = "Ravi"   p[2].name = "Neha"

Example 2: Walk Records with a Pointer, and a Self-Referential Structure

#include <stdio.h>

struct student
{
    int roll;
    char name[20];
    int marks;
};

struct node                  /* self-referential: has a pointer to its own type */
{
    int data;
    struct node *next;
};

int main(void)
{
    struct student s[3] = { {101, "Asha", 88}, {102, "Ravi", 72}, {103, "Neha", 95} };
    struct student *p, *best = s;
    struct node c = {30, NULL};
    struct node b = {20, &c};
    struct node a = {10, &b};
    struct node *walk;

    for (p = s; p < s + 3; p++)
    {
        printf("%d %-5s %d\n", p->roll, p->name, p->marks);
        if (p->marks > best->marks)
            best = p;
    }
    printf("Topper: %s\n", best->name);

    printf("Chain : ");
    for (walk = &a; walk != NULL; walk = walk->next)
        printf("%d -> ", walk->data);
    printf("NULL\n");

    return 0;
}

Output

101 Asha  88
102 Ravi  72
103 Neha  95
Topper: Neha
Chain : 10 -> 20 -> 30 -> NULL
  • p++ moves to the next record. p < s + 3 stops after the last one.
  • best is a pointer to the best record, so best->name gives the topper directly.
  • struct node contains struct node *next: a pointer to another node of the same type. This is a self-referential structure.
  • Following walk = walk->next from node to node until NULL is exactly how a linked list is traversed (Module 16).

Dot vs Arrow: Quick Guide

You have…UseExample
A structure variable.s.marks
A pointer to a structure->p->marks
An array element.s[i].marks
A pointer, with index.p[i].marks (p[i] is a structure)
A nested member via pointer-> then .p->dob.year

Important Points to Remember

  • Declare: struct student *p = &s;
  • p->member is the same as (*p).member.
  • *p.member is wrong: dot binds before *.
  • Dot for a variable, arrow for a pointer.
  • p + 1 moves to the next structure in an array.
  • A structure with a pointer to its own type is self-referential (linked lists).

Common Mistakes Beginners Make

MistakeWhat happensFix
p.roll with a pointerError: p is a pointerp->roll
s->roll with a variableError: s is not a pointers.roll
*p.rollError (means *(p.roll))(*p).roll or p->roll
Using p-> before p = &sWild pointer: crashInitialize the pointer
struct node { struct node next; };Error: incomplete typestruct node *next;

Structure Pointer Practice Questions

  1. What is the arrow operator? Write its equivalent using *.
  2. Why is *p.marks wrong?
  3. For struct student s[5], *p = s;, write two ways to access the marks of s[3] using p.
  4. What is a self-referential structure? Give an example.
  5. Why is passing a structure pointer to a function cheaper than passing the structure?
Show answers
  1. -> accesses a member through a structure pointer. p->x equals (*p).x.
  2. The dot binds before *, so it means *(p.marks), and p is not a structure.
  3. (p + 3)->marks and p[3].marks.
  4. A structure that contains a pointer to its own type, e.g. struct node { int data; struct node *next; };
  5. Only an address (4/8 bytes) is copied instead of the whole structure.

Coding Exercise

Write void give_bonus(struct employee *e, float percent) that increases an employee's salary using the arrow operator. Apply 20% to an employee earning 25000.

Show solution
#include <stdio.h>

struct employee
{
    char name[20];
    float salary;
};

void give_bonus(struct employee *e, float percent)
{
    e->salary += e->salary * percent / 100;
}

int main(void)
{
    struct employee emp = {"Rahul", 25000.0f};
    give_bonus(&emp, 20);
    printf("%s now earns %.2f\n", emp.name, emp.salary);
    return 0;
}
Rahul now earns 30000.00

Quick Quiz: Structures and Pointers (MCQ)

  1. p->age is equivalent to:

    A) *p.age   B) (*p).age   C) &p.age   D) p.age

  2. Operator to access a member through a structure pointer:

    A) .   B) ->   C) *   D) &

  3. If struct student *p = s; (s is an array), p + 1 points to:

    A) The next byte   B) The next member   C) s[1]   D) NULL

  4. A structure containing a pointer to its own type is called:

    A) Nested   B) Self-referential   C) Recursive union   D) Anonymous

  5. For struct student s;, which is correct?

    A) s->roll   B) s.roll   C) *s.roll   D) &s->roll

Show answers
  1. B: (*p).age.
  2. B: ->.
  3. C: s[1].
  4. B: self-referential.
  5. B: s.roll.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Structure pointerstruct student *p = &s;
Arrow operatorp->x = (*p).x
Dot vs arrowVariable → dot, pointer → arrow
*p.xWrong: dot binds first
p + 1Next structure in the array
Self-referentialHas a pointer to its own type

Frequently asked questions

What is the arrow operator in C?
-> accesses a structure member through a pointer; p->x means (*p).x.
What is the difference between . and ->?
Dot works on a structure variable; arrow works on a pointer to a structure.
What is a self-referential structure?
A structure with a member that is a pointer to the same structure type, used to build linked lists and trees.

Remember

  • Dot for the thing, arrow for the address.
  • p->x is just a short form of (*p).x.

Next Topic

Next lesson: Structures and Functions in C. You will pass structures to functions by value and by pointer, and return them.