- 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 Chain of Pointers
![]() Share with a Friend |
C Programming - C Chain of Pointers
C Chain of Pointers
A chain of pointers in C refers to a series of pointers that are linked together, with each pointer pointing to the next one in the sequence. This concept is often used in data structures like linked lists, where each element (node) contains a pointer to the next element in the list.
In the case of a chain of pointers, each pointer points to another pointer, which in turn points to another pointer, and so on. The last pointer in the chain typically points to NULL to signify the end of the chain.
Example of Chain of Pointers:
Here’s an example where we create a chain of pointers that connects several integers.
C
#include <stdio.h>
#include <stdlib.h>
int main() {
// Declare three pointer variables
int *ptr1, *ptr2, *ptr3;
// Dynamically allocate memory for three integers
ptr1 = (int *)malloc(sizeof(int));
ptr2 = (int *)malloc(sizeof(int));
ptr3 = (int *)malloc(sizeof(int));
// Check if memory allocation was successful
if (ptr1 == NULL || ptr2 == NULL || ptr3 == NULL) {
printf("Memory allocation failed!\n");
return 1; // Exit the program if memory allocation fails
}
// Assign values to the dynamically allocated memory
*ptr1 = 10;
*ptr2 = 20;
*ptr3 = 30;
// Create a chain of pointers
*ptr1 = (int)ptr2; // ptr1 points to ptr2
*ptr2 = (int)ptr3; // ptr2 points to ptr3
*ptr3 = NULL; // ptr3 points to NULL (end of the chain)
// Access the values in the chain of pointers
printf("Value at ptr1: %d\n", *ptr1);
printf("Value at ptr2: %d\n", *ptr2);
printf("Value at ptr3: %d\n", *ptr3);
// Free the dynamically allocated memory
free(ptr1);
free(ptr2);
free(ptr3);
return 0;
}
Explanation:
- We dynamically allocate memory for three integers (ptr1, ptr2, ptr3).
- We then assign values to the memory locations pointed to by these pointers.
- We form a chain by assigning ptr1 to ptr2 and ptr2 to ptr3, with ptr3 pointing to NULL to mark the end of the chain.
A More Practical Example: Chain of Pointers in Linked List
One of the most common applications of chains of pointers is in the implementation of linked lists. A linked list is a collection of nodes where each node contains a pointer to the next node.
Here's an example of a simple singly linked list:
C
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node
struct Node {
int data;
struct Node *next; // Pointer to the next node in the list
};
int main() {
// Declare three nodes
struct Node *head, *second, *third;
// Dynamically allocate memory for three nodes
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
// Assign data to nodes
head->data = 1;
second->data = 2;
third->data = 3;
// Link the nodes to form a chain
head->next = second; // First node points to second
second->next = third; // Second node points to third
third->next = NULL; // Third node points to NULL (end of the list)
// Traverse and print the linked list
struct Node *current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
// Free the dynamically allocated memory
free(head);
free(second);
free(third);
return 0;
}
Output:
1 -> 2 -> 3 -> NULL
Explanation:
- We define a Node structure with two members: data (an integer) and next (a pointer to the next node).
- We create three nodes (head, second, third) and assign data to them.
- We link the nodes to form a chain:
- The first node (head) points to the second node.
- The second node points to the third node.
- The third node points to NULL to mark the end of the list.
- We then traverse the list by following the next pointers, printing the data from each node.
Chain of Pointers for Multiple Structures:
You can also create a chain of pointers with structures. Here's an example with a linked list of structures:
C
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the linked list
struct Node {
char name[50];
struct Node *next;
};
int main() {
// Declare pointers to nodes
struct Node *head, *second, *third;
// Dynamically allocate memory for three nodes
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
// Assign values to the nodes
strcpy(head->name, "Alice");
strcpy(second->name, "Bob");
strcpy(third->name, "Charlie");
// Link the nodes to form a chain
head->next = second;
second->next = third;
third->next = NULL;
// Print the names in the linked list
struct Node *current = head;
while (current != NULL) {
printf("%s -> ", current->name);
current = current->next;
}
printf("NULL\n");
// Free the dynamically allocated memory
free(head);
free(second);
free(third);
return 0;
}
Output:
Alice -> Bob -> Charlie -> NULL
Explanation:
- We use a struct Node that contains a name and a next pointer.
- The nodes are linked in a chain just like the previous examples, but now each node contains a string (name) instead of an integer.
- The linked list is traversed, and each node's name is printed.
Key Concepts in Chain of Pointers:
- Pointer Chaining: Each pointer points to another pointer or data structure.
- Memory Allocation: You can dynamically allocate memory for each node or data structure.
- Linked Lists: A common use case for chains of pointers is in linked lists, where each node points to the next node.
- Traversal: You can traverse a chain of pointers by following the pointers, one by one.
- NULL Termination: The last pointer in the chain typically points to NULL to indicate the end.
Conclusion:
Chains of pointers are essential for managing dynamic data structures like linked lists. By linking multiple elements together using pointers, you can efficiently store and access data without knowing the exact size or memory location of the elements in advance. This flexibility makes pointers and pointer chains powerful tools in C programming.
