- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Self Referential Structures
![]() Share with a Friend |
C Programming - C Self Referential Structures
C Self-Referential Structures
A self-referential structure in C is a structure that includes a pointer to an instance of the same structure type. This type of structure is commonly used to implement dynamic data structures like linked lists, trees, and graphs.
Syntax of a Self-Referential Structure
C
struct StructureName {
data_type member1;
data_type member2;
struct StructureName *pointer;
};
In the above syntax:
- struct StructureName *pointer; is a pointer to another instance of the same structure type.
- The structure can contain other members, but one member must be a pointer to the same structure type.
Example: Self-Referential Structure
Defining a Node for a Linked List
C
#include <stdio.h>
#include <stdlib.h>
// Define a self-referential structure
struct Node {
int data;
struct Node *next; // Pointer to another Node
};
int main() {
// Creating individual nodes
struct Node *head = NULL;
struct Node *second = NULL;
struct Node *third = NULL;
// Allocate memory for nodes
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
// Initialize and link nodes
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL; // End of the list
// Print the list
struct Node *temp = head;
while (temp != NULL) {
printf("Data: %d\n", temp->data);
temp = temp->next;
}
return 0;
}
Output:
Data: 1
Data: 2
Data: 3
Applications of Self-Referential Structures
- Dynamic Data Structures:
- Linked Lists (Singly, Doubly, Circular)
- Trees (Binary Trees, Binary Search Trees, etc.)
- Graphs
- Hierarchical Data Representation: For representing parent-child relationships.
- Stacks and Queues: Implemented using linked lists.
Key Points
- Recursive Nature:
- A self-referential structure is inherently recursive because it contains a pointer to the same type.
- Memory Allocation:
- Memory for instances is dynamically allocated using functions like malloc or calloc.
- Null Terminator:
- In linked lists, the next pointer of the last node is typically set to NULL.
- Flexibility:
- Self-referential structures allow dynamic expansion and contraction of data structures at runtime.
Example: Binary Tree Node
A binary tree uses self-referential structures to define nodes with left and right child pointers.
C
#include <stdio.h>
#include <stdlib.h>
// Define a binary tree node
struct TreeNode {
int data;
struct TreeNode *left;
struct TreeNode *right;
};
int main() {
// Create nodes
struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
struct TreeNode *leftChild = (struct TreeNode *)malloc(sizeof(struct TreeNode));
struct TreeNode *rightChild = (struct TreeNode *)malloc(sizeof(struct TreeNode));
// Initialize nodes
root->data = 10;
root->left = leftChild;
root->right = rightChild;
leftChild->data = 5;
leftChild->left = NULL;
leftChild->right = NULL;
rightChild->data = 15;
rightChild->left = NULL;
rightChild->right = NULL;
// Print tree structure
printf("Root: %d\n", root->data);
printf("Left Child: %d\n", root->left->data);
printf("Right Child: %d\n", root->right->data);
return 0;
}
Output:
Root: 10
Left Child: 5
Right Child: 15
Advantages of Self-Referential Structures
- Efficient Memory Utilization: Memory can be allocated and freed dynamically.
- Flexibility: Supports dynamic and complex data structures.
- Ease of Representation: Simplifies representation of hierarchical or sequential data.
Disadvantages
- Complexity in Management:
- Proper management of pointers is crucial to avoid memory leaks and segmentation faults.
- Overhead: Requires additional memory for pointers in the structure.
Conclusion
Self-referential structures are a cornerstone of dynamic and hierarchical data representation in C. Understanding how to implement and use them is essential for building efficient algorithms and data structures.
