C Notes
Complete guide to pointers to structures in C — arrow operator (->), dynamic struct allocation, passing structs by pointer, linked list basics, practical examples with output.
Passing large structures by value copies every byte — wasteful for a 200-byte struct. Pointers to structures solve this by passing just an 8-byte address, plus they enable dynamic allocation, linked data structures, and in-place modification. The arrow operator (->) makes accessing members through a pointer clean and readable.
Basic Pointer to Structure
#include <stdio.h>
struct Point {
int x, y;
};
int main() {
struct Point p = {10, 20};
struct Point *ptr = &p; // Pointer to the structure
// Access using arrow operator (->)
printf("Using ->: (%d, %d)\n", ptr->x, ptr->y);
// Equivalent: dereference then dot
printf("Using (*). : (%d, %d)\n", (*ptr).x, (*ptr).y);
// Modify through pointer
ptr->x = 99;
ptr->y = 88;
printf("After modify: (%d, %d)\n", p.x, p.y);
return 0;
}Using ->: (10, 20) Using (*). : (10, 20) After modify: (99, 88)
Arrow Operator vs Dot Operator
| Syntax | When to Use | Example |
|---|---|---|
var.member | When you have the struct directly | student.name |
ptr->member | When you have a pointer to struct | ptr->name |
(*ptr).member | Same as -> but more verbose | (*ptr).name |
The -> operator is just syntactic sugar: ptr->member is exactly equivalent to (*ptr).member.
Passing Structures by Pointer
Before raise: ID: 101 | Name: Sarah Connor | Salary: $75000.00 After 15% raise: ID: 101 | Name: Sarah Connor | Salary: $86250.00
Dynamic Allocation of Structures
Library: "The C Programming Language" by K&R (1978) - $45.99 "Clean Code" by Robert Martin (2008) - $39.99 "SICP" by Abelson & Sussman (1985) - $55.00
Simple Linked List with Struct Pointers
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next; // Self-referential pointer
};
struct Node *create_node(int value) {
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
if (node == NULL) return NULL;
node->data = value;
node->next = NULL;
return node;
}
void print_list(struct Node *head) {
struct Node *current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void free_list(struct Node *head) {
struct Node *temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
int main() {
// Build: 10 -> 20 -> 30 -> NULL
struct Node *head = create_node(10);
head->next = create_node(20);
head->next->next = create_node(30);
printf("Linked List: ");
print_list(head);
free_list(head);
return 0;
}Linked List: 10 -> 20 -> 30 -> NULL
Array of Struct Pointers
Alice scored 95 Bob scored 82 Charlie scored 91
Memory Diagram: Pointer vs Embedded
Embedded struct (value)
┌──────────────────────────────┐
│ struct Employee emp; │
│ ┌────┬────────────┬────────┐ │
│ │ id │ name[50] │ salary │ │
│ └────┴────────────┴────────┘ │
└──────────────────────────────┘
Pointer to struct
┌──────────────┐ ┌────┬────────────┬────────┐
│ struct Emp *p │──────→│ id │ name[50] │ salary │ (on heap)
└──────────────┘ └────┴────────────┴────────┘
(8 bytes) (56+ bytes on heap)
Interview Questions
Q1: Why use pointers to structures instead of passing by value?
Efficiency: passing a 200-byte struct by value copies all 200 bytes onto the stack. A pointer is only 8 bytes (on 64-bit). Pointers also allow the function to modify the original struct. For read-only access, use const struct *.
Q2: What is a self-referential structure?
A structure that contains a pointer to its own type: struct Node { int data; struct Node *next; };. This can't be a direct member (infinite size), only a pointer. It's the basis for linked lists, trees, and graphs.
Q3: What happens if you forget to dereference a struct pointer?
You get a compilation error. ptr.member won't compile because ptr is a pointer, not a struct. You must use ptr->member or (*ptr).member.
Q4: Can you have a pointer to a struct that hasn't been fully defined yet?
Yes! This is called an opaque pointer or forward declaration. You declare struct Widget; and use struct Widget *ptr without knowing the struct's contents. The full definition is hidden in the implementation file — this is the basis of information hiding in C.
Summary
- Use
->(arrow operator) to access members through a pointer ptr->memberis equivalent to(*ptr).member- Pass large structures by pointer for efficiency
- Use
const struct *for read-only pointer parameters - Dynamic allocation with malloc enables flexible, heap-based structures
- Self-referential struct pointers enable linked lists, trees, and graphs
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Pointers to Structures in C – Arrow Operator.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, structures, and, unions, pointers, pointers to structures in c – arrow operator
Related C Programming Topics