Structure with Pointers : This beginner-friendly article explains the powerful combination of structures and pointers in C programming. You’ll learn how to define and use pointers to structures, access structure members with the arrow operator (->
), dynamically allocate memory for structures using malloc
, and pass structures by reference to functions. With easy-to-understand code examples, common interview questions, and practical tips, this guide is perfect for anyone preparing for C programming interviews or trying to strengthen their understanding of pointers and structs. Whether you’re a student or an embedded developer, this article builds a solid foundation in handling structures efficiently using pointers.
Structure with Pointers
Basics Recap: Structure with Pointers Definition and Access
struct Student {
int id;
char name[20];
};
Without Structure with Pointers:
struct Student s1 = {101, "Alice"};
printf("%d", s1.id);
With Structure with Pointers:
struct Student s1 = {101, "Alice"};
struct Student *ptr = &s1;
printf("%d", ptr->id); // Or: (*ptr).id
Accessing Members: Dot (.
) vs Arrow (->
)
Expression | Meaning |
---|---|
ptr->id | Preferred: Dereference and access member |
(*ptr).id | Also valid, but less readable |
Using Pointers to Modify Structure with Pointers
void update(struct Student *s) {
s->id = 202;
strcpy(s->name, "Bob");
}
int main() {
struct Student s1 = {101, "Alice"};
update(&s1);
printf("%d %s", s1.id, s1.name);
}
✅ This modifies the original structure by passing pointer, not a copy.
Dynamic Memory Allocation for Structure with Pointers
#include <stdlib.h>
struct Student *s = (struct Student *)malloc(sizeof(struct Student));
s->id = 1;
strcpy(s->name, "Charlie");
free(s);
Why use dynamic allocation?
- When the number of objects isn’t known at compile time
- Saves stack memory
- Useful in data structures (linked list, tree)
Array of Structures with Pointers
struct Student students[3] = {
{101, "Alice"},
{102, "Bob"},
{103, "Eve"}
};
struct Student *ptr = students;
for (int i = 0; i < 3; i++) {
printf("ID: %d, Name: %s\n", (ptr + i)->id, (ptr + i)->name);
}
✅ ptr + i
gives address of each element; use ->
to access fields.
Self-Referencing Structure (Linked List Use Case)
struct Node {
int data;
struct Node *next;
};
✅ Used to build:
- Linked lists
- Trees
- Graphs
struct Node *head = (struct Node *)malloc(sizeof(struct Node));
head->data = 10;
head->next = NULL;
Pointer to Array of Structures (Advanced Usage)
struct Student *arr = malloc(3 * sizeof(struct Student));
arr[0].id = 1;
strcpy(arr[0].name, "A");
✅ Use arr[i]
or (*(arr + i)).id
Key Mistakes to Avoid Structure with Pointers
Mistake | Fix |
---|---|
Using structure pointer without initialization | Always assign memory first |
Forgetting -> when using pointer | Use ptr->member , not ptr.member |
Not freeing dynamically allocated structure | Always use free() after malloc() |
Passing structure by value when large in size | Use pointer to avoid stack overhead |
Accessing freed pointer | Set to NULL after freeing |
Summary Table
Concept | Example | Purpose |
---|---|---|
Pointer to structure | struct *ptr = &obj; | Efficient access/modification |
Dynamic allocation | malloc(sizeof(struct)) | Runtime memory usage |
Arrow operator -> | ptr->field | Shortcut for pointer access |
Self-referencing structure | struct Node *next; | Linked list, tree, stack, queue |
Array of struct pointers | struct *arr[n]; | Complex data structures |
Memory-mapped I/O with struct | (struct *)0xADDR | Embedded register mapping |
1. Basic Structure Without Pointer
#include <stdio.h>
struct Student {
int id;
char name[20];
float marks;
};
int main() {
struct Student s1 = {101, "John", 87.5};
printf("ID: %d\n", s1.id);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
2. Pointer to Structure: The Basics
To access members using a pointer to a structure, we use the ->
(arrow) operator.
#include <stdio.h>
struct Student {
int id;
float marks;
};
int main() {
struct Student s1 = {102, 91.5};
struct Student *ptr = &s1;
printf("ID: %d\n", ptr->id); // Equivalent to (*ptr).id
printf("Marks: %.2f\n", ptr->marks);
return 0;
}
🔍 Interview Tip: Explain both ptr->id
and (*ptr).id
. They are functionally the same.
3. Dynamically Allocating Structure Using malloc()
#include <stdio.h>
#include <stdlib.h>
struct Student {
int id;
float marks;
};
int main() {
struct Student *ptr = (struct Student *)malloc(sizeof(struct Student));
ptr->id = 103;
ptr->marks = 75.5;
printf("ID: %d\n", ptr->id);
printf("Marks: %.2f\n", ptr->marks);
free(ptr); // Always free memory
return 0;
}
🔍 Interview Tip: You may be asked to dynamically create an array of structures.
4. Passing Structure Pointer to Function
#include <stdio.h>
struct Student {
int id;
float marks;
};
void display(struct Student *ptr) {
printf("ID: %d\n", ptr->id);
printf("Marks: %.2f\n", ptr->marks);
}
int main() {
struct Student s = {104, 89.0};
display(&s); // Pass address
return 0;
}
🔍 Interview Tip: This is memory-efficient compared to passing the full structure.
5. Array of Structures with Pointers
#include <stdio.h>
struct Student {
int id;
float marks;
};
int main() {
struct Student students[2] = {
{201, 76.5},
{202, 88.0}
};
struct Student *ptr = students; // Point to first element
for (int i = 0; i < 2; i++) {
printf("ID: %d, Marks: %.2f\n", ptr->id, ptr->marks);
ptr++;
}
return 0;
}
6. Nested Structures with Pointers
#include <stdio.h>
struct Date {
int day, month, year;
};
struct Student {
int id;
struct Date dob;
};
int main() {
struct Student s = {301, {10, 6, 2000}};
struct Student *ptr = &s;
printf("DOB: %02d-%02d-%d\n", ptr->dob.day, ptr->dob.month, ptr->dob.year);
return 0;
}
Interview Questions on Structure with Pointers
- What is the difference between
ptr->x
and(*ptr).x
?- Both are the same.
ptr->x
is syntactic sugar for(*ptr).x
.
- Both are the same.
- Why use structure pointers instead of structures in functions?
- Efficient: avoids copying large structure data.
- Can we allocate memory dynamically for structure?
- Yes, using
malloc
.
- Yes, using
- How to create a linked list using structure pointer?
- Each node has a structure with a pointer to the next node.
- What happens if we dereference an uninitialized pointer to struct?
- It leads to undefined behavior (usually segmentation fault).
Basic Level
- What is a pointer to a structure?
- How do you access members of a structure using a pointer?
- What is the difference between
(*ptr).member
andptr->member
?
Intermediate Level
- How do you dynamically allocate memory for a structure?
- What happens if you pass a structure to a function by value vs by pointer?
- How can you use pointers to modify structure data inside a function?
- Explain how you access an array of structures using a pointer.
- Can you pass a pointer to a structure as a function parameter? Show an example.
Advanced Level
- What is a self-referential structure? How is it used in linked lists?
- How do you handle a structure containing dynamically allocated members?
- What happens when you access a structure pointer without initializing it?
- What is the memory layout difference between structure and structure pointer?
- How do you implement a generic structure pointer that can point to multiple types (with void pointers)?
- How is pointer to structure used in memory-mapped I/O (e.g., in embedded systems)?
Real-World/Embedded Examples
- Explain how you would map a peripheral register block to a structure pointer.
#define UART_BASE 0x40001000
struct UART {
volatile uint32_t DATA;
volatile uint32_t STATUS;
};
#define UART0 ((struct UART *)UART_BASE)
- Why is
volatile
used with structure pointers in embedded systems?
Practice Problems
- Write a program to create and print an array of
n
students using dynamic allocation. - Implement a linked list using self-referential structures.
- Write a function that swaps two structure variables using pointers.
- Write a function that returns a pointer to a dynamically created structure.
- Simulate a memory-mapped I/O register set using structure pointers in C.
You can also Visit other tutorials of Embedded Prep
- Structures in c
- Master How to Use a Saleae Logic Analyzer with Arduino Uno (2025)
- Top 30+ I2C Interview Questions
- Bit Manipulation Interview Questions
- Structure and Union in c
- Fault Handling in Arm Cortex Mx Processor
- Merge sort algorithm
Special thanks to @mr-raj for contributing to this article on EmbeddedPrep
Leave a Reply