Explore the best Structures and Unions Interview Questions with 50+ powerful, beginner-friendly to advanced examples. Boost your C skills and prepare with confidence.
If you’re preparing for a C or embedded systems interview, you must be ready for Structures and Unions Interview Questions. These topics look simple, but interviewers ask surprisingly tricky problems around memory layout, padding, alignment, nested usage, and differences between structures vs unions.
Whether you’re a beginner learning C or an experienced engineer brushing up for a system-level role, this guide gives you crystal-clear explanations, real examples, and practical scenarios that hiring managers love to ask .
If you’re learning C programming and want to strengthen your fundamentals before practicing Structures and Unions Interview Questions, you should explore this detailed beginner-friendly guide on structures: Structures in C – Complete Guide It explains structure syntax, memory layout, padding, nesting, and real-world examples in a very simple way. Reading this will give you a strong foundation before jumping into advanced interview questions.
Introduction of Structures and Unions Interview Questions
C is powerful because it gives you full control over memory. With that power comes responsibility and interviewers test whether you truly understand memory behavior.
Two of the biggest tools in C for organizing and controlling memory layouts are:
Structure
Union
This article focuses on all levels of structured interview questions and answers, especially tailored for c interview questions on structures and unions that appear in companies like Qualcomm, STMicroelectronics, Samsung, NXP, KPIT, TCS, and more.
What Are Structures?
In simple words:
A structure in C is a way to group different variables under one name.
Each member of a structure gets its own memory.
Example
struct Employee {
int id;
float salary;
char grade;
};
Memory is allocated for all members separately.
What Are Unions?
A union is like a structure, but all variables share the same memory location.
Think of it like a single container used by multiple items—only one item can exist meaningfully at a time.
Example
union Data {
int id;
float value;
char ch;
};
Size = size of largest member.
This simple difference is the root of almost all structure and union interview questions.
Structures vs Unions
Let’s break it down:
Feature
Structure
Union
Memory
Separate memory for each member
Shared memory for all
Size
Sum of all members (with padding)
Size of largest member
Usage
Store multiple values at once
Store one value at a time
Example Use
Employee records
Embedded protocol decoding
You’ll often hear the term unions vs structures in C in interviews when discussing memory behavior.
Beginner-Level Structures and Unions Interview Questions
Let’s warm up with friendly, simple questions.
What is a structure in C?
A structure is a user-defined datatype that groups different datatypes under one name.
Great for modeling real-world things like employee, car, sensor, etc.
What is a union in C?
A union is similar to a structure, but all members share the same memory.
This is the simplest form of union questions asked in interviews.
Why do we use structures?
To store data logically and keep multiple variables grouped.
Why do we use unions?
To save memory, especially in:
Embedded systems
Protocol parsers
Hardware registers
This is common in questions on unions in interviews.
What is the size of a structure and union?
Structure → sum of members + padding
Union → size of largest member
Can a structure contain another structure?
Yes. This leads to nested structures, a common part of structural questions examples.
Can a union contain another union?
Yes, but rarely used unless memory reuse is needed.
Intermediate-Level Structure and Union Interview Questions
Here we get into deeper concepts like padding, alignment, pointers, typedef, and passing structures to functions.
How does padding affect structures?
C aligns structure members to the CPU’s natural alignment. This affects size and layout.
Example:
struct test {
char a; // 1 byte
int b; // 4 bytes
};
Size is 8 bytes, not 5, because of alignment.
Can we remove structure padding?
Yes, using:
#pragma pack(1)
__attribute__((packed)) (GCC)
How to pass a structure to a function?
Three ways:
By value
By pointer
By reference using pointer
Can we compare two structures directly?
No. You must compare member-wise.
What happens if you read one union member after writing another?
This is undefined behavior.
This comes up in structures and unions in C interview questions and answers.
How does a union save memory?
Because it allocates memory only once for the largest member.
When should you choose structures vs unions
Use structure when you need multiple values at the same time. Use union when you need different interpretations of the same memory block.
Advanced Structures and Unions Interview Questions and Answers
These are the tricky questions interviewers love to ask.
Explain memory layout of structure vs union
Structure:
| a | b | c |
Union:
| shared memory |
What is a flexible array member?
Used to create dynamic-sized structure:
struct packet {
int size;
char data[];
};
Why do embedded systems use unions for register mapping?
Because hardware registers can be accessed in multiple formats.
When you’re learning data structures, you’ll often hear about Linked Lists. A Doubly Linked List (DLL) is a powerful and flexible variation of the basic Singly Linked List that allows you to traverse in both directions — forward and backward.
In this beginner-friendly article, we’ll explore:
What is a Doubly Linked List?
How it works (with diagrams)
Real-world uses
Advantages over singly linked list
Basic operations (insertion, deletion, traversal)
Code example in C/C++
🚀 Whether you’re preparing for coding interviews or improving your DSA skills, understanding doubly linked lists is a must!
What is a Doubly Linked List?
A Doubly Linked List is a type of linear data structure made up of nodes, where each node contains:
Data (the actual value)
Pointer to the next node
Pointer to the previous node
⚙️ Node Structure of Doubly Linked List:
+-------+----------+----------+
| prev | data | next |
+-------+----------+----------+
Each node is connected to its previous and next node, unlike a singly linked list, which only has a next pointer.
Visual Representation of Doubly Linked List
Let’s say we have 3 nodes with data: 10, 20, and 30.
Two-way traversal: You can navigate forward and backward.
Easier deletion: You can delete a node without needing the previous node pointer.
More flexible: Especially useful in complex data structures like Deques, Undo/Redo systems, Music Playlists, etc.
Disadvantages of Doubly Linked List
More memory: Each node requires extra space for the prev pointer.
More complex: Handling pointers can be error-prone for beginners.
Real-Life Applications of Doubly Linked List
Web browsers (backward and forward navigation)
Music playlist apps
Text editors (undo/redo operations)
Task managers (circular doubly linked lists for round-robin)
Conclusion DLL
Master Doubly Linked List in Data Structures (With Examples) 2025
The Doubly Linked List is an essential topic in Data Structures and Algorithms (DSA). It offers a better way to manage dynamic data, especially when two-way traversal is needed. Though slightly more complex than a singly linked list, the benefits are powerful.
Whether you’re preparing for a coding interview, building a system-level application, or just learning C programming, mastering DLLs will give you a strong foundation in memory management and pointer logic.
FAQs of Doubly Linked List
Q1: Is doubly linked list faster than singly linked list? Ans: DLL allows backward traversal and easier deletion, but it uses more memory. It’s more flexible, not necessarily faster.
Q2: Where is DLL used in real life? Ans: Browser history, playlist navigation, undo/redo features, and memory management systems.
Q3: What is the main drawback of doubly linked list? Ans: It uses extra memory due to the additional prev pointer and increases code complexity.
Q4: How is a doubly linked list different from a circular doubly linked list? Ans: In a doubly linked list, the last node’s next pointer is NULL, whereas in a circular doubly linked list, the last node’s next points back to the head, forming a circle.
Q5: Can a doubly linked list be empty? Ans: Yes, initially a doubly linked list can be empty with head pointing to NULL.
Q6: How do you insert a node at the beginning of a doubly linked list? Ans: Create a new node, point its next to the current head, set the head’s prev to the new node, and then update head to the new node.
Q7: What happens when you delete the last node in a doubly linked list? Ans: The second last node’s next pointer is set to NULL, and the last node is freed from memory.
Q8: Is it possible to implement a stack using a doubly linked list? Ans: Yes, a doubly linked list can be used to implement a stack with push and pop operations.
Q9: How does traversal in a doubly linked list work? Ans: You can traverse forward from head to NULL using next pointers or backward from the last node to head using prev pointers.
Q10: What are the memory requirements for a doubly linked list node? Ans: Each node stores data plus two pointers (prev and next), so it uses more memory compared to a singly linked list node, which stores only one pointer.
Q11: Can doubly linked lists have cycles? How to detect them? Ans: Yes, cycles can exist if nodes point back to earlier nodes. Cycle detection algorithms like Floyd’s Cycle-Finding Algorithm (tortoise and hare) can detect cycles.
Q12: Are doubly linked lists used in databases? Ans: Yes, doubly linked lists are used to implement certain data structures like LRU caches, which are common in databases and operating systems.
Q13: How do you reverse a doubly linked list? Ans: Swap the prev and next pointers for each node and update the head to the last node.
You can also Visit other tutorials of Embedded Prep
Master structures and unions in C with powerful tips for 2026. Learn memory layout, use cases, best practices, and common mistakes in C programming.
It’s a quiet evening, and I’m sitting at my desk with a cup of coffee. The soft hum of my laptop fills the room as I revisit my old C projects. I remember the first time I encountered structures and unions in C — the concepts seemed powerful but complex. Over time, I realized that mastering them wasn’t just about writing code; it was about understanding how they shape memory, improve efficiency, and make programs robust. In 2025, with embedded systems growing more advanced, having a solid grasp on these concepts is more important than ever. This guide will share 5 powerful tips to help you master structures and unions in C for success in your projects and interviews.
Structure in c
Structure and Union in c : A structure in C is a user-defined data type that groups related variables of different data types under a single name. Each member of a structure has its own memory location.
Syntax of Structure in c
struct Student {
int id;
char name[50];
float marks;
};
A unionis a user-defined data type similar to a structure, but in a union, all members share the same memory location. The size of the union is equal to the size of the largest member.
Syntax of Union in c
union Data {
int id;
char name[50];
float marks;
};
Only one member can hold a value at a time.
Accessed using the dot (.) operator.
Example of union in c
#include <stdio.h>
union Data {
int id;
char name[50];
float marks;
};
int main() {
union Data d1;
d1.id = 101;
printf(\"ID: %d\\n\", d1.id);
d1.marks = 85.5; // Overwrites \'id\' value
printf(\"Marks: %.2f\\n\", d1.marks);
return 0;
}
Difference Between Structure and Union
Feature
Structure
Union
Memory
Each member has its own memory.
All members share the same memory.
Size
Sum of all members\’ sizes.
Size of the largest member.
Storage
Stores multiple values at a time.
Stores only one value at a time.
Access
All members can be accessed independently.
Only the last assigned member holds a valid value.
Use Case
Used when all members need to hold values.
Used when only one value is needed at a time to save memory.
Pointer in Structure in C
A pointer to a structure is a pointer that holds the address of a structure variable. This allows efficient manipulation of structure data.
Declaring a Pointer to a Structure
struct Student {
int id;
char name[50];
float marks;
};
struct Student *ptr; // Pointer to a structure
💛 Support Embedded Prep
If you find our tutorials helpful and want to support our mission of sharing high-quality embedded system knowledge, you can contribute by buying us a coffee.
Every small contribution helps us keep creating valuable content for learners like you. ☕
Thank you for your support — it truly keeps Embedded Prep growing. 💻✨
Accessing Structure Members Using Pointer
Since a structure pointer holds the address of a structure, we use the -> (arrow operator) to access its members.
A pointer to a union is similar to a pointer to a structure, but since a union shares memory among its members, only one member can hold a valid value at a time.
Declaring a Pointer to a Union
union Data {
int id;
float marks;
char name[50];
};
union Data *ptr; // Pointer to a union
Accessing Union Members Using Pointer
Since a union pointer holds the address of a union variable, we use the -> (arrow operator) to access its members.
Example
#include <stdio.h>
union Data {
int id;
float marks;
char name[50];
};
int main() {
union Data d1; // Union variable
union Data *ptr = &d1; // Pointer to union
ptr->id = 101;
printf(\"ID: %d\\n\", ptr->id);
ptr->marks = 85.5; // Overwrites id
printf(\"Marks: %.2f\\n\", ptr->marks);
return 0;
}
Output:
ID: 101
Marks: 85.50
Notice that the id value is lost when we assign marks, as they share the same memory.
Dynamic Memory Allocation for Union Pointer
We can allocate memory dynamically to a union pointer using malloc().
Example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
union Data {
int id;
float marks;
char name[50];
};
int main() {
union Data *ptr;
ptr = (union Data *)malloc(sizeof(union Data)); // Memory allocation
if (ptr == NULL) {
printf(\"Memory allocation failed\\n\");
return 1;
}
ptr->id = 102;
printf(\"ID: %d\\n\", ptr->id);
ptr->marks = 90.2; // Overwrites id
printf(\"Marks: %.2f\\n\", ptr->marks);
strcpy(ptr->name, \"Bob\"); // Overwrites marks
printf(\"Name: %s\\n\", ptr->name);
free(ptr); // Free allocated memory
return 0;
}
Array of Union Pointers
We can create an array of pointers to unions to manage multiple data records.
Example
#include <stdio.h>
union Data {
int id;
float marks;
};
int main() {
union Data d1, d2;
union Data *arr[] = {&d1, &d2}; // Array of union pointers
arr[0]->id = 101;
printf(\"ID: %d\\n\", arr[0]->id);
arr[1]->marks = 92.5;
printf(\"Marks: %.2f\\n\", arr[1]->marks);
return 0;
}
Union Pointer Inside a Structure
A structure can have a pointer to a union.
Example
#include <stdio.h>
union Data {
int id;
float marks;
};
struct Student {
int roll_no;
union Data *info; // Pointer to a union
};
int main() {
union Data d1;
d1.id = 101;
struct Student s1;
s1.roll_no = 1;
s1.info = &d1; // Assign union pointer to structure
printf(\"Roll No: %d\\n\", s1.roll_no);
printf(\"ID: %d\\n\", s1.info->id);
return 0;
}
How does the system behave when one structure variable is assigned to another?
When you assign one structure variable to another, the system performs a member-by-member (field-by-field) copy. This means every value inside the structure is copied from the source to the destination, as long as both structures belong to the same structure type.
In simple terms: All data inside the structure gets duplicated exactly, just like copying one folder into another.
Key Points
1. Deep Copy vs Shallow Copy (Important for interviews & SEO)
The assignment creates a shallow copy, not a deep copy.
But for normal data types (int, float, char arrays), shallow copy works exactly like deep copy because values get fully copied.
If the structure contains pointers, only the pointer address is copied—not the data it points to.
An anonymous structure is a structure without a name, usually defined within another structure or union.
Example:
struct {
int id;
char name[50];
} student;
7. What is a Flexible Array Member in a Structure?
A flexible array member is an array with an unspecified size at the end of a structure.
Example:
struct Data {
int length;
char buffer[]; // Flexible array member
};
8. What is a Packed Structure in C?
A packed structure reduces padding and optimizes memory usage by forcing strict alignment.
Example:
#pragma pack(1)
struct PackedData {
char a;
int b;
};
9. What is a Structure with Function Pointers?
A structure can contain function pointers to store and invoke functions dynamically.
Example:
struct Operation {
int (*add)(int, int);
};
10. How are Structures Used in Linked Lists and Trees?
Structures help in data representation for linked lists and trees using self-referential pointers.
Example (Linked List Node):
struct Node {
int data;
struct Node* next;
};
Frequently Asked Questions (FAQ) on Unions in C
1. What is a union in C?
A union is a user-defined data type where all members share the same memory location, making it memory-efficient.
Example:
union Data {
int id;
float marks;
};
2. What is typedef union and why is it used?
typedef union creates a union alias for easier use.
Example:
typedef union {
int id;
float marks;
} Data;
3. What is an Anonymous Union in C?
An anonymous union is a union without a name, directly embedded within a structure.
Example:
struct {
union {
int id;
float marks;
};
} student;
4. What is a Self-Referential Union in C?
A self-referential union contains a pointer to itself, similar to a self-referential structure.
Example:
union Node {
int data;
union Node *next;
};
5. What is a Bit Field Union in C?
A bit field union allows memory-efficient representation using bit fields.
Example:
union Status {
struct {
unsigned int ready : 1;
unsigned int error : 1;
unsigned int processing : 2;
};
int full_status;
};
6. How is a Union Used Inside a Structure?
A union can be embedded in a structure to allow flexible data representation.
Example:
struct Employee {
int empID;
union {
int salary;
float hourly_rate;
} pay;
};
7. What Happens When a Union Contains Different Data Types?
Only one member can hold valid data at a time, so assigning one member will overwrite others.
Example:
union Data {
int id;
float marks;
};
union Data d;
d.id = 100;
printf(\"%d\", d.id);
d.marks = 85.5;
printf(\"%f\", d.marks); // Overwrites previous data
8. How are Unions Used in Device Drivers?
Unions help in accessing hardware registers and memory-mapped I/O efficiently.
Example:
union Register {
struct {
unsigned int enable : 1;
unsigned int mode : 2;
unsigned int reserved : 5;
};
unsigned char reg_value;
};
9. How Does a Union Optimize Memory Usage?
Since all union members share the same memory, it reduces memory consumption compared to structures.
10. What is Type Punning Using Unions?
Type punning allows interpreting a memory region as different data types.
Example:
union Convert {
int i;
float f;
};
union Convert c;
c.i = 1065353216; // Binary representation of 1.0 in IEEE 754
printf(\"%f\", c.f); // Prints 1.0
Conclusion: Understanding Structures and Unions in C
Structures and unions are fundamental building blocks in C programming, enabling efficient data organization and memory management.
Structures (struct) allow grouping multiple variables of different data types, making them ideal for defining complex data models like linked lists, trees, and configuration settings. They offer data encapsulation, structured representation, and ease of access.
Unions (union) provide a memory-efficient alternative where multiple variables share the same memory location. They are particularly useful in scenarios where only one variable is needed at a time, such as device drivers, low-level memory handling, and hardware register manipulation.
Key Takeaways
Use structures when you need to store and access multiple fields independently. Use unions when memory optimization is critical, and only one field needs to be accessed at a time. Both structures and unions support pointers, typedef, nesting, bit fields, and function pointers, making them versatile tools for embedded systems, networking, and system programming. Understanding how to leverage these data types efficiently can lead to optimized memory usage, better performance, and scalable software design.
Mastering structures and unions will significantly enhance your C programming skills, especially if you work in embedded systems, low-level programming, or high-performance applications. Keep exploring, experimenting, and optimizing!
Structures and unions in C are user-defined data types that allow grouping of variables. Structures group variables of different data types under a single name, with each member having its own memory location. Unions, on the other hand, allow storing different data types in the same memory location, with the size of the union being equal to its largest member.
Frequently Asked Questions (FAQ) on Structures and Unions in C
1. What is a structure in C? A structure in C is a user-defined data type that groups related variables of different data types under a single name. Each member of a structure has its own memory location.
2. How do you define a structure in C? A structure is defined using the struct keyword, followed by the structure name and a block containing the member declarations. For example:
struct Student {
int id;
char name[50];
float marks;
};
3. How do you access members of a structure? Members of a structure are accessed using the dot (.) operator with the structure variable. For example:
struct Student s1;
s1.id = 101;
4. What is a union in C? A union in C is a user-defined data type similar to a structure, but in a union, all members share the same memory location. This means only one member can hold a value at any given time.
5. How do you define a union in C? A union is defined using the union keyword, followed by the union name and a block containing the member declarations. For example:
union Data {
int id;
char name[50];
float marks;
};
6. How do you access members of a union? Members of a union are accessed using the dot (.) operator with the union variable. For example:
union Data d1;
d1.id = 101;
7. What is the main difference between structures and unions? The main difference is in memory allocation. In structures, each member has its own memory location, allowing multiple members to hold values simultaneously. In unions, all members share the same memory location, so only one member can hold a value at a time.
8. How do you declare a pointer to a structure? A pointer to a structure is declared by specifying the structure type followed by an asterisk (*) and the pointer name. For example:
struct Student *ptr;
9. How do you access structure members using a pointer? When using a pointer to a structure, members are accessed using the arrow (->) operator. For example:
10. How do you dynamically allocate memory for a structure pointer? Dynamic memory allocation for a structure pointer is done using the malloc function. For example:
11. Can structures contain pointers to themselves? Yes, structures can contain pointers to themselves, enabling the creation of complex data structures like linked lists. For example:
struct Node {
int data;
struct Node *next;
};
12. Can unions be nested within structures? Yes, unions can be nested within structures to create flexible data structures. For example:
struct Container {
int type;
union {
int intValue;
float floatValue;
char *stringValue;
} data;
};
13. What are bit fields in structures? Bit fields allow the allocation of a specific number of bits to structure members, enabling memory-efficient storage of data requiring limited bit-width. For example:
struct {
unsigned int flag : 1;
unsigned int value : 4;
} bitField;
14. How do you initialize structures and unions? Structures and unions can be initialized at the time of declaration. For structures:
struct Student s1 = {101, \"Alice\", 85.5};
For unions:
union Data d1 = {101};
15. What are anonymous unions? Anonymous unions are unions without a name, allowing their members to be accessed directly without a union variable. They are typically used within structures. For example:
struct Example {
int type;
union {
int intValue;
float floatValue;
};
};
16. What are the use cases for structures and unions? Structures are used when multiple related variables of different types need to be grouped together, each holding its own value. Unions are used when a variable may hold different types of data at different times, conserving memory by sharing the same memory location.
Thank you for exploring this tutorial! Stay ahead in embedded systems with expert insights, hands-on projects, and in-depth guides. Follow Embedded Prep for the latest trends, best practices, and step-by-step tutorials to enhance your expertise. Keep learning, keep innovating!