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).memberneeds 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
struct student *p = &s;stores the address ofsin p.- All three lines print the same data:
s.roll,(*p).rollandp->rollare the same member. p->marks += 5;changess.marks, because p points to s.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 + 3stops after the last one.bestis a pointer to the best record, sobest->namegives the topper directly.struct nodecontainsstruct node *next: a pointer to another node of the same type. This is a self-referential structure.- Following
walk = walk->nextfrom node to node until NULL is exactly how a linked list is traversed (Module 16).
Dot vs Arrow: Quick Guide
| You have… | Use | Example |
|---|---|---|
| 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.memberis wrong: dot binds before *.- Dot for a variable, arrow for a pointer.
p + 1moves 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
| Mistake | What happens | Fix |
|---|---|---|
p.roll with a pointer | Error: p is a pointer | p->roll |
s->roll with a variable | Error: s is not a pointer | s.roll |
*p.roll | Error (means *(p.roll)) | (*p).roll or p->roll |
Using p-> before p = &s | Wild pointer: crash | Initialize the pointer |
struct node { struct node next; }; | Error: incomplete type | struct node *next; |
Structure Pointer Practice Questions
- What is the arrow operator? Write its equivalent using
*. - Why is
*p.markswrong? - For
struct student s[5], *p = s;, write two ways to access the marks ofs[3]using p. - What is a self-referential structure? Give an example.
- Why is passing a structure pointer to a function cheaper than passing the structure?
Show answers
->accesses a member through a structure pointer.p->xequals(*p).x.- The dot binds before
*, so it means*(p.marks), and p is not a structure. (p + 3)->marksandp[3].marks.- A structure that contains a pointer to its own type, e.g.
struct node { int data; struct node *next; }; - 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)
-
p->ageis equivalent to:A)
*p.ageB)(*p).ageC)&p.ageD)p.age -
Operator to access a member through a structure pointer:
A)
.B)->C)*D)& -
If
struct student *p = s;(s is an array),p + 1points to:A) The next byte B) The next member C)
s[1]D) NULL -
A structure containing a pointer to its own type is called:
A) Nested B) Self-referential C) Recursive union D) Anonymous
-
For
struct student s;, which is correct?A)
s->rollB)s.rollC)*s.rollD)&s->roll
Show answers
- B:
(*p).age. - B:
->. - C:
s[1]. - B: self-referential.
- B:
s.roll.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Structure pointer | struct student *p = &s; |
|---|---|
| Arrow operator | p->x = (*p).x |
| Dot vs arrow | Variable → dot, pointer → arrow |
| *p.x | Wrong: dot binds first |
| p + 1 | Next structure in the array |
| Self-referential | Has 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.