C Programs Tutorials | IT Developer
IT Developer

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:

  1. Pointer Chaining: Each pointer points to another pointer or data structure.
  2. Memory Allocation: You can dynamically allocate memory for each node or data structure.
  3. Linked Lists: A common use case for chains of pointers is in linked lists, where each node points to the next node.
  4. Traversal: You can traverse a chain of pointers by following the pointers, one by one.
  5. 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.