<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bhagavan on Medium]]></title>
        <description><![CDATA[Stories by Bhagavan on Medium]]></description>
        <link>https://medium.com/@suryabhagavanchakkapalli?source=rss-b7f76858627c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jookNpoOc6-rIv5epx8sIw.jpeg</url>
            <title>Stories by Bhagavan on Medium</title>
            <link>https://medium.com/@suryabhagavanchakkapalli?source=rss-b7f76858627c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:17:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suryabhagavanchakkapalli/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[DSA in C — Part 14: Reverse a Linked List (Iterative & Recursive)]]></title>
            <link>https://medium.com/@suryabhagavanchakkapalli/dsa-in-c-part-14-reverse-a-linked-list-iterative-recursive-add53fe5e788?source=rss-b7f76858627c------2</link>
            <guid isPermaLink="false">https://medium.com/p/add53fe5e788</guid>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[c-programming-language]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[reverse-a-linked-list]]></category>
            <dc:creator><![CDATA[Bhagavan]]></dc:creator>
            <pubDate>Tue, 24 Mar 2026 16:43:49 GMT</pubDate>
            <atom:updated>2026-03-24T16:43:49.958Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In the previous article, we learned how to:</p><ul><li>traverse a linked list</li><li>search elements</li><li>count nodes</li></ul><p>Now we move to one of the <strong>most important and frequently asked problems</strong>:</p><p>👉 <strong>Reversing a Linked List</strong></p><p>Reversing means changing the direction of links between nodes.</p><h3>Problem Statement</h3><p>Given a linked list:</p><pre>10 → 20 → 30 → 40 → NULL</pre><p>After reversing:</p><pre>40 → 30 → 20 → 10 → NULL</pre><h3>Structure of Node</h3><pre>struct Node {<br>    int data;<br>    struct Node *next;<br>};</pre><h3>Key Idea</h3><p>In a linked list, each node points to the next node.</p><p>To reverse the list, we need to <strong>reverse these links</strong>.</p><h3>Iterative Approach (Most Important 🔥)</h3><h3>Concept</h3><p>We use three pointers:</p><ul><li>prev → previous node</li><li>curr → current node</li><li>next → next node</li></ul><h3>Visualization</h3><p>Before:</p><pre>prev   curr   next<br>NULL → 10 → 20 → 30 → 40</pre><p>Step-by-step reversal:</p><pre>Step 1: 10 → NULL<br>Step 2: 20 → 10<br>Step 3: 30 → 20 → 10<br>Step 4: 40 → 30 → 20 → 10</pre><h3>Code (Iterative)</h3><pre>struct Node* reverse(struct Node *head) {<br><br>    struct Node *prev = NULL;<br><br>    struct Node *curr = head;<br><br>    struct Node *next = NULL;<br><br>    while (curr != NULL) {<br>        next = curr-&gt;next;<br>        curr-&gt;next = prev;<br><br>        prev = curr;<br>        curr = next;<br>    }<br><br>    return prev;<br>}</pre><h3>Recursive Approach</h3><h3>Concept</h3><p>The recursive approach works by:</p><ul><li>Reversing the remaining list</li><li>fixing the current node</li></ul><h3>Code (Recursive)</h3><pre>struct Node* reverseRecursive(struct Node *head) {<br><br>    if (head == NULL || head-&gt;next == NULL)<br>        return head;<br><br>    struct Node *newHead = reverseRecursive(head-&gt;next);<br><br>    head-&gt;next-&gt;next = head;<br>    head-&gt;next = NULL;<br><br>    return newHead;<br>}</pre><h3>Traversal Function</h3><pre>void display(struct Node *head) {<br>    struct Node *temp = head;<br><br>    while (temp != NULL) {<br>        printf(&quot;%d &quot;, temp-&gt;data);<br>        temp = temp-&gt;next;<br>    }<br>}</pre><h3>Complete Program</h3><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>struct Node {<br>    int data;<br>    struct Node *next;<br>};<br><br>void display(struct Node *head) {<br>    struct Node *temp = head;<br><br>    while (temp != NULL) {<br>        printf(&quot;%d &quot;, temp-&gt;data);<br>        temp = temp-&gt;next;<br>    }<br>}<br><br>struct Node* insertEnd(struct Node *head, int data) {<br><br>    struct Node *newNode = malloc(sizeof(struct Node));<br><br>    newNode-&gt;data = data;<br>    newNode-&gt;next = NULL;<br><br>    if (head == NULL)<br>        return newNode;<br><br>    struct Node *temp = head;<br><br>    while (temp-&gt;next != NULL)<br>        temp = temp-&gt;next;<br><br>    temp-&gt;next = newNode;<br><br>    return head;<br>}<br><br>struct Node* reverse(struct Node *head) {<br>    struct Node *prev = NULL;<br>    struct Node *curr = head;<br>    struct Node *next = NULL;<br><br>    while (curr != NULL) {<br>        next = curr-&gt;next;<br>        curr-&gt;next = prev;<br>        prev = curr;<br>        curr = next;<br>    }<br><br>    return prev;<br>}<br><br>int main() {<br>    struct Node *head = NULL;<br><br>    head = insertEnd(head, 10);<br>    head = insertEnd(head, 20);<br>    head = insertEnd(head, 30);<br>    head = insertEnd(head, 40);<br><br>    printf(&quot;Original List: &quot;);<br><br>    display(head);<br><br>    head = reverse(head);<br><br>    printf(&quot;\nReversed List: &quot;);<br><br>    display(head);<br><br>    return 0;<br>}</pre><h3>Output</h3><pre>Original List: 10 20 30 40<br>Reversed List: 40 30 20 10</pre><h3>Time Complexity</h3><p>Approach Time Space</p><p>Iterative O(n) O(1)</p><p>Recursive O(n) O(n)</p><h3>Conclusion</h3><p>In this article, we learned how to reverse a linked list using:</p><ul><li>iterative approach (most efficient)</li><li>recursive approach</li></ul><p>This is one of the <strong>most important linked list problems</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=add53fe5e788" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA in C — Part 13: Linked List Traversal, Searching, and Counting]]></title>
            <link>https://medium.com/@suryabhagavanchakkapalli/dsa-in-c-part-13-linked-list-traversal-searching-and-counting-1af4e7351479?source=rss-b7f76858627c------2</link>
            <guid isPermaLink="false">https://medium.com/p/1af4e7351479</guid>
            <category><![CDATA[c-language]]></category>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[singly-linked-list]]></category>
            <dc:creator><![CDATA[Bhagavan]]></dc:creator>
            <pubDate>Tue, 24 Mar 2026 16:42:52 GMT</pubDate>
            <atom:updated>2026-03-24T16:42:52.897Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In the previous articles, we learned how to:</p><ul><li>create linked lists</li><li>insert nodes</li><li>delete nodes</li></ul><p>Now, to effectively work with linked lists, we must know how to <strong>access and process the elements</strong> stored in them.</p><p>This is done using:</p><ul><li><strong>Traversal</strong> → visiting each node</li><li><strong>Searching</strong> → finding a specific element</li><li><strong>Counting</strong> → calculating the number of nodes</li></ul><p>These operations are fundamental and are used in almost every linked list problem.</p><h3>Structure of a Node</h3><pre>struct Node {<br>    int data;<br>    struct Node *next;<br>};</pre><h3>Linked List Traversal</h3><p>Traversal means visiting each node of the list one by one.</p><h3>Steps</h3><ol><li>Start from the head</li><li>Visit the current node</li><li>Move to the next node</li><li>Repeat until NULL</li></ol><h3>Example</h3><pre>head → 10 → 20 → 30 → 40 → NULL</pre><p>Traversal output:</p><pre>10 20 30 40</pre><h3>Traversal Code</h3><pre>void display(struct Node *head) {<br>    struct Node *temp = head;<br>    <br>    while (temp != NULL) {<br>        printf(&quot;%d &quot;, temp-&gt;data);<br>        temp = temp-&gt;next;<br>    }<br>}</pre><h3>Counting Nodes in a Linked List</h3><p>Counting means finding how many nodes are present in the list.</p><h3>Logic</h3><ul><li>Traverse the list</li><li>Increment the counter for each node</li></ul><h3>Code</h3><pre>int countNodes(struct Node *head) {<br>    int count = 0;<br><br>    struct Node *temp = head;<br><br>    while (temp != NULL) {<br>        count++;<br>        temp = temp-&gt;next;<br>    }<br>    return count;<br>}</pre><h3>Searching in a Linked List</h3><p>Searching means checking whether a value exists in the list.</p><h3>Steps</h3><ol><li>Traverse the list</li><li>Compare each node’s data</li><li>If match found → return position</li><li>If not found → return -1</li></ol><h3>Searching Code</h3><pre>int search(struct Node *head, int key) {<br>    int position = 1;<br>    <br>    struct Node *temp = head;<br> <br>    while (temp != NULL) {<br>        if (temp-&gt;data == key) {<br>            return position;<br>        }<br>        temp = temp-&gt;next;<br>        position++;<br>    }<br>    <br>    return -1;<br>}</pre><h3>Example</h3><p>Linked list:</p><pre>10 → 20 → 30 → 40 → NULL</pre><p>Searching for <strong>30</strong>:</p><pre>Found at position 3</pre><p>Searching for <strong>50</strong>:</p><pre>Not found (-1)</pre><h3>Complete Program</h3><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>struct Node {<br>    int data;<br>    struct Node *next;<br>};<br><br>void display(struct Node *head) {<br><br>    struct Node *temp = head;<br><br>    while (temp != NULL) {<br>        printf(&quot;%d &quot;, temp-&gt;data);<br>        temp = temp-&gt;next;<br>    }<br>}<br><br>int countNodes(struct Node *head) {<br>    int count = 0;<br><br>    struct Node *temp = head;<br><br>    while (temp != NULL) {<br>        count++;<br>        temp = temp-&gt;next;<br>    }<br><br>    return count;<br>}<br><br>int search(struct Node *head, int key) {<br>    int pos = 1;<br><br>    struct Node *temp = head;<br><br>    while (temp != NULL) {<br>        if (temp-&gt;data == key)<br>            return pos;<br>        temp = temp-&gt;next;<br>        pos++;<br>    }<br><br>    return -1;<br>}<br><br>struct Node* insertEnd(struct Node *head, int data) {<br><br>    struct Node *newNode = malloc(sizeof(struct Node));<br><br>    newNode-&gt;data = data;<br>    newNode-&gt;next = NULL;<br><br>    if (head == NULL)<br>        return newNode;<br><br>    struct Node *temp = head;<br><br>    while (temp-&gt;next != NULL)<br>        temp = temp-&gt;next;<br>    temp-&gt;next = newNode;<br><br>    return head;<br>}<br><br>int main() {<br>    struct Node *head = NULL;<br><br>    head = insertEnd(head, 10);<br>    head = insertEnd(head, 20);<br>    head = insertEnd(head, 30);<br>    head = insertEnd(head, 40);<br><br>    printf(&quot;Linked List: &quot;);<br>    display(head);<br>    printf(&quot;\nTotal Nodes: %d&quot;, countNodes(head));<br><br>    int pos = search(head, 30);<br><br>    if (pos != -1)<br>        printf(&quot;\nElement found at position: %d&quot;, pos);<br>    else<br>        printf(&quot;\nElement not found&quot;);<br><br>    return 0;<br>}</pre><h3>Output</h3><pre>Linked List: 10 20 30 40<br>Total Nodes: 4<br>Element found at position: 3</pre><h3>Time Complexity</h3><p>Traversal →O(n)</p><p>Counting →O(n)</p><p>Searching →O(n)</p><h3>Conclusion</h3><p>In this article, we learned how to:</p><ul><li>traverse a linked list</li><li>count the number of nodes</li><li>search for an element</li></ul><p>These operations are essential for solving many linked list problems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1af4e7351479" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA in C — Part 12: Linked List Deletion (Beginning, End, and Given Position)]]></title>
            <link>https://medium.com/@suryabhagavanchakkapalli/dsa-in-c-part-12-linked-list-deletion-beginning-end-and-given-position-41ac55e6cdf0?source=rss-b7f76858627c------2</link>
            <guid isPermaLink="false">https://medium.com/p/41ac55e6cdf0</guid>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[dsa-courses]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[dynamic-programming]]></category>
            <category><![CDATA[linked-lists]]></category>
            <dc:creator><![CDATA[Bhagavan]]></dc:creator>
            <pubDate>Mon, 23 Mar 2026 16:34:48 GMT</pubDate>
            <atom:updated>2026-03-23T16:36:46.478Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In the previous article, we learned how to <strong>insert nodes</strong> into a linked list at different positions.</p><p>Now, we move to another fundamental operation: <strong>deletion</strong>.</p><p>Deletion is important because in real applications, data is not only added but also <strong>removed dynamically</strong>.</p><p>In this article, we will learn how to delete nodes in three ways:</p><ul><li>Deletion at the beginning</li><li>Deletion at the end</li><li>Deletion at a given position</li></ul><p>Understanding deletion requires careful handling of pointers to avoid memory leaks and invalid access.</p><h3>Structure of a Linked List Node</h3><pre>struct Node {<br>    int data;<br>    struct Node *next;<br>};</pre><h3>Deletion at the Beginning</h3><p>This is the simplest deletion operation.</p><h3>Steps</h3><ol><li>Check if the list is empty</li><li>Store the current head in a temporary pointer</li><li>Move the head to the next node</li><li>Free the old head</li></ol><h3>Example</h3><p>Before deletion:</p><pre>head → 10 → 20 → 30 → NULL</pre><p>After deleting the first node:</p><pre>head → 20 → 30 → NULL</pre><h3>Code</h3><pre>struct Node* deleteBeginning(struct Node *head) {<br><br>    if (head == NULL)<br>        return NULL;<br><br>    struct Node *temp = head;<br><br>    head = head-&gt;next;<br>    free(temp);<br><br>    return head;<br>}</pre><p>Time Complexity:</p><pre>O(1)</pre><h3>Deletion at the End</h3><p>In this method, the last node is removed.</p><h3>Steps</h3><ol><li>Traverse to the second last node</li><li>Free the last node</li><li>Set the second last node’s next to NULL</li></ol><h3>Example</h3><p>Before deletion:</p><pre>10 → 20 → 30 → NULL</pre><p>After deleting the last node:</p><pre>10 → 20 → NULL</pre><h3>Code</h3><pre>struct Node* deleteEnd(struct Node *head) {<br><br>    if (head == NULL)<br>        return NULL;<br><br>    if (head-&gt;next == NULL) {<br>        free(head);<br>        return NULL;<br>    }<br><br>    struct Node *temp = head;<br><br>    while (temp-&gt;next-&gt;next != NULL) {<br>        temp = temp-&gt;next;<br>    }<br><br>    free(temp-&gt;next);<br>    temp-&gt;next = NULL;<br><br>    return head;<br>}</pre><p>Time Complexity:</p><pre>O(n)</pre><h3>Deletion at a Given Position</h3><p>This operation removes a node at a specific position.</p><h3>Steps</h3><ol><li>Traverse to the node before the position</li><li>Store the node to be deleted</li><li>Adjust pointers</li><li>Free the node</li></ol><h3>Example</h3><p>Before deletion:</p><pre>10 → 20 → 30 → 40 → NULL</pre><p>Delete node at position 3:</p><pre>10 → 20 → 40 → NULL</pre><h3>Code</h3><pre>struct Node* deletePosition(struct Node *head, int pos) {<br><br>    if (head == NULL)<br>        return NULL;<br><br>    if (pos == 1) {<br><br>        struct Node *temp = head;<br><br>        head = head-&gt;next;<br>        free(temp);<br><br>        return head;<br>    }<br><br>    struct Node *temp = head;<br><br>    for (int i = 1; i &lt; pos - 1; i++) {<br>        temp = temp-&gt;next;<br>    }<br><br>    struct Node *nodeToDelete = temp-&gt;next;<br><br>    temp-&gt;next = nodeToDelete-&gt;next;<br>    free(nodeToDelete);<br><br>    return head;<br>}</pre><p>Time Complexity:</p><pre>O(n)</pre><h3>Traversal Function</h3><pre>void display(struct Node *head) {<br> <br>   struct Node *temp = head;<br><br>    while (temp != NULL) {<br>        printf(&quot;%d &quot;, temp-&gt;data);<br>        temp = temp-&gt;next;<br>    }<br>}</pre><h3>Complete Program</h3><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>struct Node {<br>    int data;<br>    struct Node *next;<br>};<br><br>void display(struct Node *head) {<br><br>    struct Node *temp = head;<br><br>    while (temp != NULL) {<br>        printf(&quot;%d &quot;, temp-&gt;data);<br>        temp = temp-&gt;next;<br>    }<br>}<br><br>struct Node* insertEnd(struct Node *head, int data) {<br><br>    struct Node *newNode = malloc(sizeof(struct Node));<br><br>    newNode-&gt;data = data;<br>    newNode-&gt;next = NULL;<br><br>    if (head == NULL)<br>        return newNode;<br><br>    struct Node *temp = head;<br><br>    while (temp-&gt;next != NULL) {<br>        temp = temp-&gt;next;<br>    }<br><br>    temp-&gt;next = newNode;<br>    return head;<br>}<br><br>struct Node* deleteBeginning(struct Node *head) {<br><br>    if (head == NULL)<br>        return NULL;<br><br>    struct Node *temp = head;<br><br>    head = head-&gt;next;<br>    free(temp);<br><br>    return head;<br>}<br><br>struct Node* deleteEnd(struct Node *head) {<br><br>    if (head == NULL)<br>        return NULL;<br><br>    if (head-&gt;next == NULL) {<br>        free(head);<br>        return NULL;<br>    }<br><br>    struct Node *temp = head;<br><br>    while (temp-&gt;next-&gt;next != NULL) {<br>        temp = temp-&gt;next;<br>    }<br><br>    free(temp-&gt;next);<br>    temp-&gt;next = NULL;<br><br>    return head;<br>}<br><br>struct Node* deletePosition(struct Node *head, int pos) {<br><br>    if (head == NULL)<br>        return NULL;<br><br>    if (pos == 1) {<br>        struct Node *temp = head;<br><br>        head = head-&gt;next;<br>        free(temp);<br><br>        return head;<br>    }<br>    <br>    struct Node *temp = head;<br>     <br>    for (int i = 1; i &lt; pos - 1; i++) {<br>        temp = temp-&gt;next;<br>    }<br>    <br>    struct Node *nodeToDelete = temp-&gt;next;<br>    <br>    temp-&gt;next = nodeToDelete-&gt;next;<br>    free(nodeToDelete);<br>    <br>    return head;<br>}<br><br>int main() {<br><br>    struct Node *head = NULL;<br><br>    head = insertEnd(head, 10);<br>    head = insertEnd(head, 20);<br>    head = insertEnd(head, 30);<br>    head = insertEnd(head, 40);<br><br>    printf(&quot;Original List: &quot;);<br>    display(head);<br><br>    head = deleteBeginning(head);<br>    printf(&quot;\nAfter deleting beginning: &quot;);<br>    display(head);<br><br>    head = deleteEnd(head);<br>    printf(&quot;\nAfter deleting end: &quot;);<br>    display(head);<br><br>    head = deletePosition(head, 2);<br>    printf(&quot;\nAfter deleting position 2: &quot;);<br>    display(head);<br> <br>    return 0;<br>}</pre><h3>Output</h3><pre>Original List: 10 20 30 40<br>After deleting beginning: 20 30 40<br>After deleting end: 20 30<br>After deleting position 2: 20</pre><h3>Time Complexity Summary</h3><p>Delete at beginning → O(1)</p><p>Delete at end →O(n)</p><p>Delete at position →O(n)</p><h3>Interview Practice Questions</h3><h3>Question 1</h3><p>Why is deletion at the beginning <strong>O(1)</strong>?</p><h3>Question 2</h3><p>Why do we need to traverse the list for deletion at the end?</p><h3>Question 3</h3><p>What happens if we try to delete from an empty list?</p><h3>Question 4</h3><p>What is the role of free() In deletion?</p><h3>Question 5</h3><p>What error occurs if we do not update pointers correctly?</p><h3>Conclusion</h3><p>In this article, we learned how to perform <strong>deletion operations in a linked list</strong>.</p><p>We covered:</p><ul><li>deletion at the beginning</li><li>deletion at the end</li><li>deletion at a given position</li></ul><p>These operations are essential for managing linked lists dynamically.</p><p>In the next article, we will explore <strong>search and traversal techniques</strong>, followed by advanced problems such as <strong>reversing a linked list</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41ac55e6cdf0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA in C — Part 11: Linked List Insertion (Beginning, End, and Given Position)]]></title>
            <link>https://medium.com/@suryabhagavanchakkapalli/dsa-in-c-part-11-linked-list-insertion-beginning-end-and-given-position-5313e0de9690?source=rss-b7f76858627c------2</link>
            <guid isPermaLink="false">https://medium.com/p/5313e0de9690</guid>
            <category><![CDATA[dsa-courses]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[pointers]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[singly-linked-list]]></category>
            <dc:creator><![CDATA[Bhagavan]]></dc:creator>
            <pubDate>Mon, 23 Mar 2026 16:12:09 GMT</pubDate>
            <atom:updated>2026-03-23T16:12:09.839Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In the previous article, we introduced the <strong>concept of Linked Lists</strong> and learned how nodes are created and connected using pointers.</p><p>Unlike arrays, linked lists allow <strong>dynamic memory allocation</strong>, which means elements can be inserted or removed during program execution without resizing the entire structure.</p><p>To work effectively with linked lists, we must understand the basic operations performed on them. One of the most important operations is <strong>insertion</strong>.</p><p>In this article, we will learn how to insert nodes in three different ways:</p><ul><li>Insertion at the beginning</li><li>Insertion at the end</li><li>Insertion at a given position</li></ul><p>Understanding these operations will help us manage linked lists efficiently and prepare us for more advanced operations like deletion and reversal.</p><h3>Structure of a Linked List Node</h3><p>A linked list is made up of <strong>nodes</strong>. Each node contains two parts:</p><ol><li>Data</li><li>Pointer to the next node</li></ol><h3>Node Structure</h3><pre>struct Node {<br>    int data;<br>    struct Node *next;<br>};</pre><p>Example representation of a linked list:</p><pre>[10 | next] → [20 | next] → [30 | next] → NULL</pre><p>The last node always points to <strong>NULL</strong>, indicating the end of the list.</p><h3>Insertion at the Beginning</h3><p>Insertion at the beginning is the <strong>simplest and fastest</strong> operation.</p><h3>Steps</h3><ol><li>Create a new node</li><li>Assign the data to the node</li><li>Make the new node point to the current head</li><li>Update the head pointer</li></ol><h3>Example</h3><p>Before insertion:</p><pre>head → 20 → 30 → NULL</pre><p>After inserting <strong>10</strong>:</p><pre>head → 10 → 20 → 30 → NULL</pre><h3>Code</h3><pre>struct Node* insertBeginning(struct Node *head, int data) {<br><br>    struct Node *newNode = malloc(sizeof(struct Node));<br><br>    newNode-&gt;data = data;<br>    newNode-&gt;next = head;<br>    head = newNode;<br><br>    return head;<br>}</pre><p>Time Complexity:</p><pre>O(1)</pre><h3>Insertion at the End</h3><p>In this method, the new node is added after the <strong>last node</strong> of the list.</p><h3>Steps</h3><ol><li>Create a new node</li><li>Traverse the list until the last node</li><li>Set the last node’s next pointer to the new node</li></ol><h3>Example</h3><p>Before insertion:</p><pre>10 → 20 → 30 → NULL</pre><p>After inserting <strong>40</strong>:</p><pre>10 → 20 → 30 → 40 → NULL</pre><h3>Code</h3><pre>struct Node* insertEnd(struct Node *head, int data) {<br><br>    struct Node *newNode = malloc(sizeof(struct Node));<br><br>    newNode-&gt;data = data;<br>    newNode-&gt;next = NULL;<br><br>    if (head == NULL)<br>        return newNode;<br><br>    struct Node *temp = head;<br><br>    while (temp-&gt;next != NULL) {<br>        temp = temp-&gt;next;<br>    }<br><br>    temp-&gt;next = newNode;<br><br>    return head;<br>}</pre><pre>Time Complexity:<br>O(n)</pre><h3>Insertion at a Given Position</h3><p>Sometimes we need to insert a node at a <strong>specific position</strong> in the linked list.</p><h3>Steps</h3><ol><li>Traverse the list until the node before the desired position</li><li>Create the new node</li><li>Adjust the pointers carefully</li></ol><h3>Example</h3><p>Before insertion:</p><pre>10 → 20 → 30 → NULL</pre><p>Insert <strong>25</strong> at position <strong>3</strong>:</p><pre>10 → 20 → 25 → 30 → NULL</pre><h3>Code</h3><pre>struct Node* insertPosition(struct Node *head, int data, int pos) {<br><br>    struct Node *newNode = malloc(sizeof(struct Node));<br><br>    newNode-&gt;data = data;<br><br>    struct Node *temp = head;<br><br>    for (int i = 1; i &lt; pos - 1; i++) {<br>        temp = temp-&gt;next;<br>    }<br><br>    newNode-&gt;next = temp-&gt;next;<br>    temp-&gt;next = newNode;<br><br>    return head;<br>}</pre><p>Time Complexity:</p><pre>O(n)</pre><h3>Traversing the Linked List</h3><p>To display elements of the linked list, we traverse from the <strong>head node</strong> until we reach NULL.</p><h3>Code</h3><pre>void display(struct Node *head) {<br> <br>   struct Node *temp = head;<br><br>    while (temp != NULL) {<br>        printf(&quot;%d &quot;, temp-&gt;data);<br>        temp = temp-&gt;next;<br>    }<br><br>}</pre><h3>Complete Program</h3><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>struct Node {<br>    int data;<br>    struct Node *next;<br>};<br><br>void display(struct Node *head) {<br><br>    struct Node *temp = head;<br><br>    while (temp != NULL) {<br>        printf(&quot;%d &quot;, temp-&gt;data);<br>        temp = temp-&gt;next;<br>    }<br>}<br><br>struct Node* insertBeginning(struct Node *head, int data) {<br><br>    struct Node *newNode = malloc(sizeof(struct Node));<br><br>    newNode-&gt;data = data;<br>    newNode-&gt;next = head;<br><br>    head = newNode;<br>    return head;<br>}<br><br>struct Node* insertEnd(struct Node *head, int data) {<br><br>    struct Node *newNode = malloc(sizeof(struct Node));<br><br>    newNode-&gt;data = data;<br>    newNode-&gt;next = NULL;<br><br>    if (head == NULL)<br>        return newNode;<br><br>    struct Node *temp = head;<br><br>    while (temp-&gt;next != NULL) {<br>        temp = temp-&gt;next;<br>    }<br><br>    temp-&gt;next = newNode;<br><br>    return head;<br>}<br><br>struct Node* insertPosition(struct Node *head, int data, int pos) {<br><br>    struct Node *newNode = malloc(sizeof(struct Node));<br><br>    newNode-&gt;data = data;<br><br>    struct Node *temp = head;<br><br>    for (int i = 1; i &lt; pos - 1; i++) {<br>        temp = temp-&gt;next;<br>    }<br><br>    newNode-&gt;next = temp-&gt;next;<br>    temp-&gt;next = newNode;<br><br>    return head;<br>}<br><br>int main() {<br><br>    struct Node *head = NULL;<br><br>    head = insertBeginning(head, 20);<br>    head = insertBeginning(head, 10);<br>    head = insertEnd(head, 30);<br>    head = insertEnd(head, 40);<br>    head = insertPosition(head, 25, 3);<br> <br>   display(head);<br>    return 0;<br>}</pre><h3>Output</h3><pre>10 20 25 30 40</pre><h3>Time Complexity Summary</h3><p>OperationComplexityInsert at beginningO(1)Insert at endO(n)Insert at positionO(n)</p><h3>Interview Practice Questions</h3><h3>Question 1</h3><p>Why is insertion at the beginning of a linked list <strong>O(1)</strong>?</p><h3>Question 2</h3><p>Why does insertion at the end of a linked list require <strong>O(n)</strong> time?</p><h3>Question 3</h3><p>What happens if the head pointer is NULL during insertion?</p><h3>Question 4</h3><p>Write a function to insert a node at <strong>position 2</strong> in a linked list.</p><h3>Question 5</h3><p>How does linked list insertion differ from array insertion?</p><h3>Conclusion</h3><p>In this article, we explored <strong>insertion operations in a linked list</strong>.</p><p>We learned how to:</p><ul><li>Insert a node at the beginning</li><li>insert a node at the end</li><li>insert a node at a specific position</li></ul><p>These operations form the foundation for working with linked lists.</p><p>In the next article, we will explore <strong>Linked List Deletion</strong>, where nodes are removed dynamically from the list.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5313e0de9690" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA in C- Part10: Linked Lists in C — Introduction and Basic Implementation]]></title>
            <link>https://medium.com/@suryabhagavanchakkapalli/dsa-in-c-part10-linked-lists-in-c-introduction-and-basic-implementation-b08a13920280?source=rss-b7f76858627c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b08a13920280</guid>
            <category><![CDATA[dsa-training]]></category>
            <category><![CDATA[dsa-courses]]></category>
            <category><![CDATA[pointers]]></category>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[c-programming]]></category>
            <dc:creator><![CDATA[Bhagavan]]></dc:creator>
            <pubDate>Fri, 13 Mar 2026 16:30:57 GMT</pubDate>
            <atom:updated>2026-03-21T15:16:20.452Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Until now, in this series, we have studied many core concepts of C programming:</p><ul><li>memory layout</li><li>pointers</li><li>pointer arithmetic</li><li>arrays</li><li>strings</li><li>dynamic memory allocation</li><li>structures</li></ul><p>All these topics build the foundation required to understand <strong>data structures</strong>.</p><p>The first and most important pointer-based data structure is the <strong>Linked List</strong>.</p><p>Arrays have a major limitation: their size is fixed once they are created. If we need more space later, we must create a new array and copy all elements, which is inefficient.</p><p>Linked lists solve this problem by using <strong>dynamic memory allocation</strong>. Instead of storing elements in contiguous memory locations like arrays, linked lists store elements in <strong>separate memory blocks connected using pointers</strong>.</p><p>Each element in a linked list is called a <strong>node</strong>.</p><h3>What is a Linked List</h3><p>A <strong>linked list</strong> is a collection of nodes where each node contains:</p><ul><li>data</li><li>pointer to the next node</li></ul><p>Conceptually:</p><pre>node1 → node2 → node3 → node4 → NULL</pre><p>Each node stores the address of the next node.</p><p>The last node points to <strong>NULL</strong>, which indicates the end of the list.</p><h3>Structure of a Node</h3><p>In C, nodes are usually implemented using structures.</p><p>Example:</p><pre>struct Node<br>{<br>    int data;<br>    struct Node *next;<br>};</pre><p>Explanation:</p><pre>data → stores the value<br>next → pointer to the next node</pre><h3>Creating a Node Dynamically</h3><p>Nodes are created using <strong>dynamic memory allocation</strong>.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>struct Node<br>{<br>    int data;<br>    struct Node *next;<br>};<br><br>int main()<br>{<br>    struct Node *n1;<br>    n1 = (struct Node*)malloc(sizeof(struct Node));<br>    n1-&gt;data = 10;<br>    n1-&gt;next = NULL;<br>    printf(&quot;%d\n&quot;, n1-&gt;data);<br>    free(n1);<br>    return 0;<br>}</pre><h3>Creating Multiple Nodes</h3><p>Example:</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>struct Node<br>{<br>    int data;<br>    struct Node *next;<br>};<br><br>int main()<br>{<br>    struct Node *n1,*n2,*n3;<br>    n1 = (struct Node*)malloc(sizeof(struct Node));<br>    n2 = (struct Node*)malloc(sizeof(struct Node));<br>    n3 = (struct Node*)malloc(sizeof(struct Node));<br>    n1-&gt;data = 10;<br>    n2-&gt;data = 20;<br>    n3-&gt;data = 30;<br>    n1-&gt;next = n2;<br>    n2-&gt;next = n3;<br>    n3-&gt;next = NULL;<br>    printf(&quot;%d\n&quot;, n1-&gt;data);<br>    printf(&quot;%d\n&quot;, n2-&gt;data);<br>    printf(&quot;%d\n&quot;, n3-&gt;data);<br>    free(n1);<br>    free(n2);<br>    free(n3);<br>    return 0;<br>}</pre><h3>Memory Representation</h3><p>Suppose nodes contain values:</p><pre>10 → 20 → 30</pre><p>Memory layout:</p><pre>Address      Data      Next<br>1000         10        2000<br>2000         20        3000<br>3000         30        NULL</pre><p>Notice that the nodes are <strong>not necessarily stored in contiguous memory locations</strong>.</p><h3>Traversing a Linked List</h3><p>To print elements of a linked list, we traverse the list starting from the first node.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>struct Node<br>{<br>    int data;<br>    struct Node *next;<br>};<br><br>void display(struct Node *head)<br>{<br>    struct Node *temp = head;<br>    while(temp != NULL)<br>    {<br>        printf(&quot;%d &quot;, temp-&gt;data);<br>        temp = temp-&gt;next;<br>    }<br>}<br><br>int main()<br>{<br>    struct Node *n1,*n2,*n3;<br>    n1 = malloc(sizeof(struct Node));<br>    n2 = malloc(sizeof(struct Node));<br>    n3 = malloc(sizeof(struct Node));<br>    n1-&gt;data = 10;<br>    n2-&gt;data = 20;<br>    n3-&gt;data = 30;<br>    n1-&gt;next = n2;<br>    n2-&gt;next = n3;<br>    n3-&gt;next = NULL;<br>    display(n1);<br>    free(n1);<br>    free(n2);<br>    free(n3);<br>    return 0;<br>}</pre><p>Output:</p><pre>10 20 30</pre><h3>Head Pointer</h3><p>The <strong>head pointer</strong> stores the address of the first node.</p><p>Example:</p><pre>head → node1 → node2 → node3 → NULL</pre><p>If the head becomes NULL, the list is empty.</p><h3>Advantages of Linked Lists</h3><p>Linked lists provide several advantages over arrays.</p><h3>Dynamic Size</h3><p>Linked lists can grow or shrink during program execution.</p><h3>Efficient Insertions</h3><p>Insertion of new nodes does not require shifting elements like arrays.</p><h3>Memory Utilization</h3><p>Memory is allocated only when needed.</p><h3>Disadvantages of Linked Lists</h3><h3>Extra Memory</h3><p>Each node requires additional memory for storing the pointer.</p><h3>No Random Access</h3><p>Elements cannot be accessed directly by index like arrays.</p><p>Traversal must start from the beginning.</p><h3>Types of Linked Lists</h3><p>There are several types of linked lists.</p><h3>Singly Linked List</h3><p>Each node points to the next node.</p><pre>node → node → node → NULL</pre><h3>Doubly Linked List</h3><p>Each node has two pointers.</p><pre>prev ← node → next</pre><h3>Circular Linked List</h3><p>Last node points back to the first node.</p><pre>node → node → node<br> ↑             ↓<br> └─────────────┘</pre><h3>Interview Practice Questions</h3><h3>Question 1</h3><p>What are the two components of a linked list node?</p><h3>Question 2</h3><p>What does the last node of a linked list point to?</p><h3>Question 3</h3><p>What will the following code print?</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>struct Node<br>{<br>    int data;<br>    struct Node *next;<br>};<br><br>int main()<br>{<br>    struct Node *n;<br>    n = malloc(sizeof(struct Node));<br>    n-&gt;data = 15;<br>    n-&gt;next = NULL;<br>    printf(&quot;%d\n&quot;, n-&gt;data);<br>    free(n);<br>    return 0;<br>}</pre><h3>Question 4</h3><p>Why are linked lists preferred over arrays in some applications?</p><h3>Question 5</h3><p>Which memory region is used to allocate linked list nodes?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b08a13920280" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA in C- Part9: Structures in C — Creating Custom Data Types]]></title>
            <link>https://medium.com/@suryabhagavanchakkapalli/dsa-in-c-part9-structures-in-c-creating-custom-data-types-0bd0c93bc871?source=rss-b7f76858627c------2</link>
            <guid isPermaLink="false">https://medium.com/p/0bd0c93bc871</guid>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[structure-in-c]]></category>
            <category><![CDATA[pointers-in-c]]></category>
            <category><![CDATA[dsa-training]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Bhagavan]]></dc:creator>
            <pubDate>Thu, 12 Mar 2026 16:08:51 GMT</pubDate>
            <atom:updated>2026-03-21T15:16:37.388Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>So far in this series, we have explored many core concepts of C programming, including:</p><ul><li>memory layout</li><li>pointers</li><li>pointer arithmetic</li><li>arrays</li><li>strings</li><li>dynamic memory allocation</li></ul><p>All these concepts prepare us for implementing real-world programs and data structures. However, another important feature of C is the ability to group different types of data together.</p><p>In many situations, a single variable is not enough to represent real-world entities.</p><p>For example, consider storing information about a student:</p><ul><li>student ID</li><li>name</li><li>marks</li></ul><p>Using separate variables for each attribute becomes difficult to manage when dealing with many students. To solve this problem, C provides a powerful feature called <strong>structures</strong>.</p><p>A <strong>structure</strong> allows us to combine multiple variables of different data types into a single unit.</p><p>Structures are widely used in C programming and form the foundation for many data structures, such as:</p><ul><li>linked lists</li><li>trees</li><li>graphs</li></ul><h3>What is a Structure</h3><p>A <strong>structure</strong> is a user-defined data type that allows grouping variables of different types under one name.</p><p>Syntax:</p><pre>struct structure_name<br>{<br>    datatype member1;<br>    datatype member2;<br>    datatype member3;<br>};</pre><p>Example:</p><pre>struct Student<br>{<br>    int id;<br>    char name[50];<br>    float marks;<br>};</pre><p>Here, Student is a structure containing three members:</p><ul><li>id</li><li>name</li><li>marks</li></ul><h3>Declaring Structure Variables</h3><p>Once the structure is defined, we can create variables of that structure type.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>struct Student<br>{<br>    int id;<br>    float marks;<br>};<br>int main()<br>{<br>    struct Student s1;<br>    s1.id = 101;<br>    s1.marks = 85.5;<br>    printf(&quot;%d\n&quot;, s1.id);<br>    printf(&quot;%f\n&quot;, s1.marks);<br>    return 0;<br>}</pre><p>Output:</p><pre>101<br>85.500000</pre><p>The <strong>dot operator (</strong><strong>.)</strong> is used to access structure members.</p><h3>Structure with Multiple Members</h3><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>struct Student<br>{<br>    int id;<br>    char name[20];<br>    float marks;<br>};<br>int main()<br>{<br>    struct Student s1;<br>    s1.id = 1;<br>    s1.marks = 90.5;<br>    printf(&quot;ID: %d\n&quot;, s1.id);<br>    printf(&quot;Marks: %f\n&quot;, s1.marks);<br>    return 0;<br>}</pre><p>Here, the structure groups related data into one variable.</p><h3>Initializing a Structure</h3><p>Structures can also be initialized during declaration.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>struct Student<br>{<br>    int id;<br>    float marks;<br>};<br>int main()<br>{<br>    struct Student s1 = {101, 88.5};<br>    printf(&quot;%d\n&quot;, s1.id);<br>    printf(&quot;%f\n&quot;, s1.marks);<br>    return 0;<br>}</pre><p>Output:</p><pre>101<br>88.500000</pre><h3>Array of Structures</h3><p>Often, we need to store multiple records of the same type.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>struct Student<br>{<br>    int id;<br>    float marks;<br>};<br><br>int main()<br>{<br>    struct Student s[3];<br>    for(int i=0;i&lt;3;i++)<br>    {<br>        printf(&quot;Enter ID and Marks: &quot;);<br>        scanf(&quot;%d %f&quot;, &amp;s[i].id, &amp;s[i].marks);<br>    }<br>    for(int i=0;i&lt;3;i++)<br>    {<br>        printf(&quot;%d %.2f\n&quot;, s[i].id, s[i].marks);<br>    }<br>    return 0;<br>}</pre><p>This creates an <strong>array of structure variables</strong>.</p><h3>Pointer to Structure</h3><p>Structures can also be accessed using pointers.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>struct Student<br>{<br>    int id;<br>    float marks;<br>};<br>int main()<br>{<br>    struct Student s1 = {10, 92.5};<br>    struct Student *p = &amp;s1;<br>    printf(&quot;%d\n&quot;, p-&gt;id);<br>    printf(&quot;%f\n&quot;, p-&gt;marks);<br>    return 0;<br>}</pre><p>Here we use the <strong>arrow operator (</strong><strong>-&gt;)</strong> to access members through a pointer.</p><p>Equivalent forms:</p><pre>(*p).id<br>p-&gt;id</pre><p>Both mean the same thing.</p><h3>Passing Structure to Functions</h3><p>Structures can be passed to functions.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>struct Student<br>{<br>    int id;<br>    float marks;<br>};<br><br>void display(struct Student s)<br>{<br>    printf(&quot;%d\n&quot;, s.id);<br>    printf(&quot;%f\n&quot;, s.marks);<br>}<br><br>int main()<br>{<br>    struct Student s1 = {101, 95.0};<br>    display(s1);<br>    return 0;<br>}</pre><p>Here, the structure is passed by <strong>value</strong>.</p><h3>Passing Structure Using a Pointer</h3><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>struct Student<br>{<br>    int id;<br>    float marks;<br>};<br><br>void modify(struct Student *s)<br>{<br>    s-&gt;marks = 100;<br>}<br><br>int main()<br>{<br>    struct Student s1 = {101, 80};<br>    modify(&amp;s1);<br>    printf(&quot;%f\n&quot;, s1.marks);<br>    return 0;<br>}</pre><p>Output:</p><pre>100.000000</pre><p>Passing a pointer allows the function to modify the original structure.</p><h3>Structure Memory Layout</h3><p>Example:</p><pre>struct Example<br>{<br>    int a;<br>    char b;<br>    float c;<br>};</pre><p>Memory representation depends on <strong>alignment and padding</strong>.</p><p>Typical sizes:</p><pre>int → 4 bytes<br>char → 1 byte<br>float → 4 bytes</pre><p>Actual structure size may be larger due to padding.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>struct Example<br>{<br>    int a;<br>    char b;<br>    float c;<br>};<br>int main()<br>{<br>    printf(&quot;%lu\n&quot;, sizeof(struct Example));<br>}</pre><h3>Why Structures Are Important</h3><p>Structures are extremely important in C programming because they allow the representation of complex data.</p><p>Examples:</p><pre>Student records<br>Employee databases<br>File metadata<br>Network packets</pre><p>Most data structures use structures to represent nodes.</p><p>Example:</p><pre>Linked List Node<br>Tree Node<br>Graph Node</pre><p>Understanding structures is essential before learning <strong>linked lists</strong>.</p><h3>Interview Practice Questions</h3><h3>Question 1</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>struct Data<br>{<br>    int a;<br>    int b;<br>};<br>int main()<br>{<br>    struct Data d = {5,10};<br>    printf(&quot;%d\n&quot;, d.a);<br>    return 0;<br>}</pre><h3>Question 2</h3><p>What will be printed?</p><pre>#include&lt;stdio.h&gt;<br><br>struct Data<br>{<br>    int x;<br>};<br>int main()<br>{<br>    struct Data d;<br>    d.x = 20;<br>    printf(&quot;%d\n&quot;, d.x);<br>    return 0;<br>}</pre><h3>Question 3</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>struct Data<br>{<br>    int a;<br>};<br><br>int main()<br>{<br>    struct Data d = {10};<br>    struct Data *p = &amp;d;<br>    printf(&quot;%d\n&quot;, p-&gt;a);<br>    return 0;<br>}</pre><h3>Question 4</h3><p>What will be printed?</p><pre>#include&lt;stdio.h&gt;<br><br>struct Test<br>{<br>    int a;<br>};<br><br>void modify(struct Test *t)<br>{<br>    t-&gt;a = 50;<br>}<br><br>int main()<br>{<br>    struct Test t = {10};<br>    modify(&amp;t);<br>    printf(&quot;%d\n&quot;, t.a);<br>    return 0;<br>}</pre><h3>Question 5</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>struct Data<br>{<br>    int a;<br>};<br><br>int main()<br>{<br>    struct Data d = {7};<br>    printf(&quot;%lu\n&quot;, sizeof(d));<br>    return 0;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0bd0c93bc871" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA in C- Part8: Dynamic Memory Allocation in C — malloc, calloc, realloc, and free]]></title>
            <link>https://medium.com/@suryabhagavanchakkapalli/dsa-in-c-part8-dynamic-memory-allocation-in-c-malloc-calloc-realloc-and-free-e2f570356b2c?source=rss-b7f76858627c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2f570356b2c</guid>
            <category><![CDATA[dynamic-memory-allocation]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[pointers-in-c]]></category>
            <category><![CDATA[c-programming]]></category>
            <dc:creator><![CDATA[Bhagavan]]></dc:creator>
            <pubDate>Thu, 12 Mar 2026 10:42:51 GMT</pubDate>
            <atom:updated>2026-03-12T10:42:51.178Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In previous articles, we explored memory layout, pointers, pointer arithmetic, arrays, pointers with functions, and strings. All those concepts lead naturally to one very important topic in C programming: <strong>Dynamic Memory Allocation (DMA)</strong>.</p><p>When we declare variables normally in C, memory is allocated automatically by the compiler. For example:</p><pre>int a;<br>int arr[10];</pre><p>These variables are stored in the <strong>stack</strong> or <strong>data segment</strong>, and their size must be known at compile time.</p><p>However, in many real-world situations, we do not know how much memory we need until the program is running. For example:</p><ul><li>reading an unknown number of inputs from a user</li><li>allocating memory for dynamic data structures like linked lists or trees</li><li>processing files where the size is not known beforehand</li></ul><p>To solve this problem, C provides <strong>Dynamic Memory Allocation</strong>, which allows us to allocate memory <strong>during program execution</strong>.</p><p>Dynamic memory allocation is performed using functions provided in the <strong>stdlib.h</strong> header file.</p><p>The four main functions used for dynamic memory allocation are:</p><ul><li>malloc()</li><li>calloc()</li><li>realloc()</li><li>free()</li></ul><p>These functions allocate and manage memory from the <strong>heap</strong>.</p><h3>Heap Memory</h3><p>Dynamic memory is allocated from a region of memory called the <strong>heap</strong>.</p><p>Important characteristics of heap memory:</p><ul><li>allocated at runtime</li><li>manually controlled by the programmer</li><li>remains allocated until explicitly freed</li><li>slower than stack memory</li></ul><p>If dynamically allocated memory is not released properly, it leads to a <strong>memory leak</strong>.</p><h3>malloc() — Memory Allocation</h3><p>The function malloc() stands for <strong>memory allocation</strong>.</p><p>It allocates a block of memory of a specified size and returns a pointer to the beginning of that memory block.</p><p>Syntax:</p><pre>pointer = malloc(size_in_bytes);</pre><p>Example:</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int *p;<br>    p = (int*)malloc(sizeof(int));<br>    *p = 10;<br>    printf(&quot;%d\n&quot;, *p);<br>    free(p);<br>    return 0;<br>}</pre><p>Explanation:</p><pre>malloc(sizeof(int))</pre><p>allocates enough memory to store one integer.</p><p>The function returns the address of the allocated memory.</p><p>If allocation fails, malloc() returns <strong>NULL</strong>.</p><h3>Allocating Memory for Multiple Elements</h3><p>Example:</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int n = 5;<br>    int *p;<br>    p = (int*)malloc(n * sizeof(int));<br>    for(int i=0;i&lt;n;i++)<br>    {<br>        p[i] = i + 1;<br>    }<br>    for(int i=0;i&lt;n;i++)<br>    {<br>        printf(&quot;%d &quot;, p[i]);<br>    }<br>    free(p);<br>    return 0;<br>}</pre><p>Output:</p><pre>1 2 3 4 5</pre><p>Here, memory is allocated dynamically for <strong>5 integers</strong>.</p><h3>calloc() — Contiguous Allocation</h3><p>The function calloc() allocates memory for multiple elements and <strong>initializes all bytes to zero</strong>.</p><p>Syntax:</p><pre>pointer = calloc(number_of_elements, size_of_each_element);</pre><p>Example:</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int *p;<br>    p = (int*)calloc(5, sizeof(int));<br>    for(int i=0;i&lt;5;i++)<br>    {<br>        printf(&quot;%d &quot;, p[i]);<br>    }<br>    free(p);<br>    return 0;<br>}</pre><p>Output:</p><pre>0 0 0 0 0</pre><p>Difference between malloc() and calloc():</p><p>FunctionInitializationmallocuninitialized memorycallocmemory initialized to zero</p><h3>realloc() — Reallocating Memory</h3><p>Sometimes the allocated memory block needs to be resized.</p><p>The function realloc() allows us to change the size of previously allocated memory.</p><p>Syntax:</p><pre>pointer = realloc(pointer, new_size);</pre><p>Example:</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int *p;<br>    p = (int*)malloc(3 * sizeof(int));<br>    for(int i=0;i&lt;3;i++)<br>    {<br>        p[i] = i + 1;<br>    }<br>    p = (int*)realloc(p, 5 * sizeof(int));<br>    for(int i=3;i&lt;5;i++)<br>    {<br>        p[i] = i + 1;<br>    }<br>    for(int i=0;i&lt;5;i++)<br>    {<br>        printf(&quot;%d &quot;, p[i]);<br>    }<br>    free(p);<br>    return 0;<br>}</pre><p>Output:</p><pre>1 2 3 4 5</pre><p>realloc() expands or shrinks the memory block.</p><h3>free() — Releasing Memory</h3><p>The function free() releases dynamically allocated memory back to the system.</p><p>Syntax:</p><pre>free(pointer);</pre><p>Example:</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int *p;<br>    p = (int*)malloc(sizeof(int));<br>    *p = 25;<br>    printf(&quot;%d\n&quot;, *p);<br>    free(p);<br>    return 0;<br>}</pre><p>After calling free(), The memory becomes available for reuse.</p><h3>Memory Leak</h3><p>A <strong>memory leak</strong> occurs when allocated memory is not released using free().</p><p>Example of a memory leak:</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int *p;<br>    p = (int*)malloc(sizeof(int));<br>    *p = 100;<br>    return 0;<br>}</pre><p>Here, memory is allocated but never freed.</p><p>Repeated leaks can eventually exhaust available memory.</p><h3>Dynamic Arrays Using malloc</h3><p>Example:</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int n;<br>    printf(&quot;Enter number of elements: &quot;);<br>    scanf(&quot;%d&quot;, &amp;n);<br>    int *arr;<br>    arr = (int*)malloc(n * sizeof(int));<br>    for(int i=0;i&lt;n;i++)<br>    {<br>        scanf(&quot;%d&quot;, &amp;arr[i]);<br>    }<br>    for(int i=0;i&lt;n;i++)<br>    {<br>        printf(&quot;%d &quot;, arr[i]);<br>    }<br>    free(arr);<br>    return 0;<br>}</pre><p>This program creates an array whose size is determined <strong>during runtime</strong>.</p><h3>Why Dynamic Memory Allocation is Important</h3><p>Dynamic memory allocation is essential for implementing many advanced data structures.</p><p>Examples include:</p><ul><li>linked lists</li><li>trees</li><li>graphs</li><li>hash tables</li><li>dynamic arrays</li></ul><p>In these structures, the number of elements is often unknown beforehand, so memory must be allocated dynamically.</p><h3>Interview Practice Questions</h3><h3>Question 1</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int *p;<br>    p = (int*)malloc(sizeof(int));<br>    *p = 20;<br>    printf(&quot;%d\n&quot;, *p);<br>    free(p);<br>    return 0;<br>}</pre><h3>Question 2</h3><p>What will be printed?</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int *p;<br>    p = (int*)calloc(3, sizeof(int));<br>    for(int i=0;i&lt;3;i++)<br>    {<br>        printf(&quot;%d &quot;, p[i]);<br>    }<br>    free(p);<br>    return 0;<br>}</pre><h3>Question 3</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int *p;<br>    p = (int*)malloc(2 * sizeof(int));<br>    p[0] = 10;<br>    p[1] = 20;<br>    p = (int*)realloc(p, 3 * sizeof(int));<br>    p[2] = 30;<br>    for(int i=0;i&lt;3;i++)<br>    {<br>        printf(&quot;%d &quot;, p[i]);<br>    }<br>    free(p);<br>    return 0;<br>}</pre><h3>Question 4</h3><p>What will happen in the following program?</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int *p;<br>    p = (int*)malloc(sizeof(int));<br>    free(p);<br>    free(p);<br>    return 0;<br>}</pre><h3>Question 5</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>int main()<br>{<br>    int *p;<br>    p = (int*)malloc(sizeof(int));<br>    *p = 50;<br>    printf(&quot;%d\n&quot;, *p);<br>    free(p);<br>    return 0;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2f570356b2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA in C- Part7: Strings in C — Understanding Character Arrays and String Pointers]]></title>
            <link>https://medium.com/@suryabhagavanchakkapalli/dsa-in-c-part7-strings-in-c-understanding-character-arrays-and-string-pointers-82728e582afa?source=rss-b7f76858627c------2</link>
            <guid isPermaLink="false">https://medium.com/p/82728e582afa</guid>
            <category><![CDATA[dsa-training]]></category>
            <category><![CDATA[pointers-in-c]]></category>
            <category><![CDATA[strings-in-c]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[c-programming]]></category>
            <dc:creator><![CDATA[Bhagavan]]></dc:creator>
            <pubDate>Wed, 11 Mar 2026 15:51:39 GMT</pubDate>
            <atom:updated>2026-03-11T15:51:39.336Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In C programming, a <strong>string</strong> is simply a sequence of characters stored in memory and terminated by a special character called the <strong>null character (</strong><strong>\0)</strong>.</p><p>Unlike many modern programming languages, C does not have a built-in string data type. Instead, strings are represented using <strong>character arrays</strong> or <strong>pointers to characters</strong>.</p><p>Understanding strings in C is very important because many real-world programs involve processing text such as:</p><ul><li>user input</li><li>file content</li><li>command-line arguments</li><li>messages and logs</li></ul><p>Strings are also closely related to <strong>pointers</strong>, since operations on strings often involve pointer manipulation.</p><p>In this article, we will explore:</p><ul><li>How strings are stored in memory</li><li>character arrays and strings</li><li>null terminator</li><li>string initialization</li><li>accessing strings using pointers</li><li>common string operations</li></ul><h3>What is a String in C</h3><p>A string in C is an array of characters followed by the <strong>null character </strong><strong>\0</strong>.</p><p>Example:</p><pre>Hello</pre><p>Internally stored as:</p><pre>H e l l o \0</pre><p>The null character marks the <strong>end of the string</strong>.</p><h3>Declaring a String Using a Character Array</h3><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str[6] = {&#39;H&#39;,&#39;e&#39;,&#39;l&#39;,&#39;l&#39;,&#39;o&#39;,&#39;\0&#39;};<br>    printf(&quot;%s\n&quot;, str);<br>    return 0;<br>}</pre><p>Output:</p><pre>Hello</pre><p>Memory representation:</p><pre>Address      Value<br>1000         H<br>1001         e<br>1002         l<br>1003         l<br>1004         o<br>1005         \0</pre><p>The null character tells the program where the string ends.</p><h3>String Initialization Using Double Quotes</h3><p>C allows a simpler way to initialize strings.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str[] = &quot;Hello&quot;;<br>    printf(&quot;%s\n&quot;, str);<br>    return 0;<br>}</pre><p>The compiler automatically adds the <strong>null character</strong>.</p><p>Memory layout:</p><pre>H e l l o \0</pre><h3>Printing Strings</h3><p>Strings are printed using the %s format specifier.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char name[] = &quot;Surya&quot;;<br>    printf(&quot;%s\n&quot;, name);<br>    return 0;<br>}</pre><p>Output:</p><pre>Surya</pre><h3>Accessing Characters in a String</h3><p>Since a string is an array, individual characters can be accessed using indexing.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str[] = &quot;Hello&quot;;<br>    printf(&quot;%c\n&quot;, str[0]);<br>    printf(&quot;%c\n&quot;, str[1]);<br>    printf(&quot;%c\n&quot;, str[2]);<br>    return 0;<br>}</pre><p>Output:</p><pre>H<br>e<br>l</pre><h3>Traversing a String Using a Loop</h3><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str[] = &quot;Hello&quot;;<br>    for(int i=0; str[i] != &#39;\0&#39;; i++)<br>    {<br>        printf(&quot;%c &quot;, str[i]);<br>    }<br>    return 0;<br>}</pre><p>Output:</p><pre>H e l l o</pre><p>The loop stops when it reaches the <strong>null character</strong>.</p><h3>Using Pointers with Strings</h3><p>Strings can also be accessed using pointers.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str[] = &quot;Hello&quot;;<br>    char *p = str;<br>    printf(&quot;%c\n&quot;, *p);<br>    printf(&quot;%c\n&quot;, *(p+1));<br>    printf(&quot;%c\n&quot;, *(p+2));<br>    return 0;<br>}</pre><p>Output:</p><pre>H<br>e<br>l</pre><p>Explanation:</p><pre>p → points to first character</pre><p>Pointer arithmetic allows us to move through the string.</p><h3>Traversing a String Using a Pointer</h3><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str[] = &quot;World&quot;;<br>    char *p = str;<br>    while(*p != &#39;\0&#39;)<br>    {<br>        printf(&quot;%c &quot;, *p);<br>        p++;<br>    }<br>    return 0;<br>}</pre><p>Output:</p><pre>W o r l d</pre><p>This method is widely used in string processing.</p><h3>Difference Between Character Array and String Pointer</h3><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str1[] = &quot;Hello&quot;;<br>    char *str2 = &quot;Hello&quot;;<br>    printf(&quot;%s\n&quot;, str1);<br>    printf(&quot;%s\n&quot;, str2);<br>    return 0;<br>}</pre><p>Important difference:</p><pre>str1 → stored in array memory<br>str2 → pointer to string literal</pre><p>Trying to modify a string literal may cause an error.</p><p>Example:</p><pre>str2[0] = &#39;h&#39;;</pre><p>This is unsafe.</p><h3>Common String Functions</h3><p>C provides several library functions for string operations in the <strong>string.h</strong> header file.</p><p>Common functions include:</p><p>FunctionPurposestrlen()Find the length of the stringstrcpy()Copy one string to anotherstrcat()Concatenate stringsstrcmp()Compare two strings</p><p>Example using strlen():</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;string.h&gt;<br><br>int main()<br>{<br>    char str[] = &quot;Hello&quot;;<br>    printf(&quot;%lu\n&quot;, strlen(str));<br>    return 0;<br>}</pre><p>Output:</p><pre>5</pre><h3>Why Strings Are Important</h3><p>Strings are used in many programming tasks, such as:</p><ul><li>user input processing</li><li>command-line arguments</li><li>file operations</li><li>data parsing</li><li>network communication</li></ul><p>Understanding how strings work internally is important for writing efficient C programs.</p><h3>Interview Practice Questions</h3><h3>Question 1</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str[] = &quot;Hello&quot;;<br>    printf(&quot;%c\n&quot;, str[1]);<br>    return 0;<br>}</pre><h3>Question 2</h3><p>What will be printed?</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str[] = &quot;World&quot;;<br>    char *p = str;<br>    printf(&quot;%c\n&quot;, *(p+3));<br>    return 0;<br>}</pre><h3>Question 3</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str[] = &quot;CProgramming&quot;;<br>    for(int i=0; str[i] != &#39;\0&#39;; i++)<br>    {<br>        printf(&quot;%c &quot;, str[i]);<br>    }<br>    return 0;<br>}</pre><h3>Question 4</h3><p>What will be printed?</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char str[] = &quot;Hello&quot;;<br>    printf(&quot;%lu\n&quot;, sizeof(str));<br>    return 0;<br>}</pre><h3>Question 5</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>int main()<br>{<br>    char *str = &quot;Hello&quot;;<br>    printf(&quot;%c\n&quot;, str[4]);<br>    return 0;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82728e582afa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA in C Part6: Pointers and Functions in C — Call by Value vs Call by Reference]]></title>
            <link>https://medium.com/@suryabhagavanchakkapalli/dsa-in-c-part6-pointers-and-functions-in-c-call-by-value-vs-call-by-reference-e0f566d209c9?source=rss-b7f76858627c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0f566d209c9</guid>
            <category><![CDATA[functions-in-c]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[dsa-training]]></category>
            <category><![CDATA[pointers-in-c]]></category>
            <dc:creator><![CDATA[Bhagavan]]></dc:creator>
            <pubDate>Wed, 11 Mar 2026 10:29:17 GMT</pubDate>
            <atom:updated>2026-03-11T10:29:17.843Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Functions are one of the most fundamental building blocks of any C program. They allow us to divide large programs into smaller, manageable units and promote code reuse.</p><p>When we pass variables to functions in C, the language follows a mechanism known as <strong>Call by Value</strong>. This means that the function receives a <strong>copy of the variable</strong>, not the original variable itself.</p><p>Because of this behavior, modifying a parameter inside a function does <strong>not affect the original variable</strong>.</p><p>However, C provides a powerful mechanism using <strong>pointers</strong> that allows functions to modify the original variables. This technique is commonly called <strong>Call by Reference</strong>.</p><p>Understanding how pointers interact with functions is extremely important because many real-world programs and data structures rely on this mechanism.</p><p>In this article, we will explore:</p><ul><li>How function arguments work in C</li><li>call by value</li><li>call by reference using pointers</li><li>modifying variables using pointers</li><li>practical examples</li></ul><h3>How Function Arguments Work in C</h3><p>Consider the following simple program.</p><pre>#include&lt;stdio.h&gt;<br><br>void modify(int x)<br>{<br>    x = 20;<br>}<br>int main()<br>{<br>    int a = 10;<br>    modify(a);<br>    printf(&quot;%d\n&quot;, a);<br>    return 0;<br>}</pre><p>Output:</p><pre>10</pre><p>Explanation:</p><p>When the function modify() is called, the value of a is <strong>copied</strong> into the parameter x.</p><p>Memory representation:</p><pre>a = 10<br>x = 10</pre><p>Inside the function, we change x to 20.</p><pre>x = 20</pre><p>However, this does not affect the original variable a.</p><p>This behavior is known as <strong>Call by Value</strong>.</p><h3>Call by Value</h3><p>In <strong>Call by Value</strong>, a function receives a copy of the argument.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>void square(int x)<br>{<br>    x = x * x;<br>}<br>int main()<br>{<br>    int num = 5;<br>    square(num);<br>    printf(&quot;%d\n&quot;, num);<br>    return 0;<br>}</pre><p>Output:</p><pre>5</pre><p>Explanation:</p><p>Even though the function computes x * x, the original variable num remains unchanged because the function only works with a <strong>copy</strong>.</p><h3>Using Pointers to Modify Variables</h3><p>To modify the original variable inside a function, we must pass its <strong>address</strong>.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>void modify(int *p)<br>{<br>    *p = 20;<br>}<br>int main()<br>{<br>    int a = 10;<br>    modify(&amp;a);<br>    printf(&quot;%d\n&quot;, a);<br>    return 0;<br>}</pre><p>Output:</p><pre>20</pre><p>Explanation:</p><pre>&amp;a → address of a<br>p  → receives address of a<br>*p → accesses the value stored at that address</pre><p>When the function executes:</p><pre>*p = 20</pre><p>the value stored at the address of a is modified.</p><p>Thus, the original variable changes.</p><h3>Call by Reference Using Pointers</h3><p>When we pass the <strong>address of a variable</strong> to a function, the function can directly modify the variable stored in memory.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>void increment(int *p)<br>{<br>    (*p)++;<br>}<br>int main()<br>{<br>    int value = 10;<br>    increment(&amp;value);<br>    printf(&quot;%d\n&quot;, value);<br>    return 0;<br>}</pre><p>Output:</p><pre>11</pre><p>Explanation:</p><pre>value → stored in memory<br>&amp;p → address of value<br>p → pointer to value</pre><p>The statement:</p><pre>(*p)++</pre><p>increments the original variable.</p><h3>Example — Swapping Two Numbers</h3><p>One of the most common examples used to demonstrate pointers with functions is <strong>swapping two variables</strong>.</p><h3>Incorrect Swap (Call by Value)</h3><pre>#include&lt;stdio.h&gt;<br><br>void swap(int a,int b)<br>{<br>    int temp;<br>    temp=a;<br>    a=b;<br>    b=temp;<br>}<br>int main()<br>{<br>    int x=10;<br>    int y=20;<br>    swap(x,y);<br>    printf(&quot;%d %d\n&quot;,x,y);<br>    return 0;<br>}</pre><p>Output:</p><pre>10 20</pre><p>The values are not swapped because the function only works with <strong>copies</strong>.</p><h3>Correct Swap (Using Pointers)</h3><pre>#include&lt;stdio.h&gt;<br><br>void swap(int *a,int *b)<br>{<br>    int temp;<br>    temp=*a;<br>    *a=*b;<br>    *b=temp;<br>}<br>int main()<br>{<br>    int x=10;<br>    int y=20;<br>    swap(&amp;x,&amp;y);<br>    printf(&quot;%d %d\n&quot;,x,y);<br>    return 0;<br>}</pre><p>Output:</p><pre>20 10</pre><p>Explanation:</p><pre>&amp;a → address of x<br>&amp;b → address of y</pre><p>Inside the function, the values stored at these addresses are exchanged.</p><h3>Memory Representation of Swap</h3><p>Before swap:</p><pre>Address      Value<br>1000         10   (x)<br>1004         20   (y)</pre><p>During swap:</p><pre>*a → 10<br>*b → 20</pre><p>After swap:</p><pre>Address      Value<br>1000         20<br>1004         10</pre><h3>Why Pointers Are Used with Functions</h3><p>Pointers allow functions to:</p><ul><li>modify original variables</li><li>return multiple values</li><li>manipulate large data structures efficiently</li><li>work directly with memory</li></ul><p>Without pointers, many data structures, such as:</p><ul><li>linked lists</li><li>trees</li><li>graphs</li></ul><p>would be extremely difficult to implement.</p><h3>Returning Multiple Values from a Function</h3><p>Pointers also allow functions to return multiple results.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>void calculate(int a,int b,int *sum,int *product)<br>{<br>    *sum = a + b;<br>    *product = a * b;<br>}<br>int main()<br>{<br>    int x=5;<br>    int y=3;<br>    int s,p;<br>    calculate(x,y,&amp;s,&amp;p);<br>    printf(&quot;Sum = %d\n&quot;,s);<br>    printf(&quot;Product = %d\n&quot;,p);<br>    return 0;<br>}</pre><p>Output:</p><pre>Sum = 8<br>Product = 15</pre><h3>Interview Practice Questions</h3><h3>Question 1</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>void change(int x)<br>{<br>    x = 50;<br>}<br>int main()<br>{<br>    int a = 10;<br>    change(a);<br>    printf(&quot;%d\n&quot;, a);<br>    return 0;<br>}</pre><h3>Question 2</h3><p>What will be printed?</p><pre>#include&lt;stdio.h&gt;<br><br>void change(int *p)<br>{<br>    *p = 30;<br>}<br>int main()<br>{<br>    int a = 5;<br>    change(&amp;a);<br>    printf(&quot;%d\n&quot;, a);<br>    return 0;<br>}</pre><h3>Question 3</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>void increment(int *x)<br>{<br>    (*x)++;<br>}<br>int main()<br>{<br>    int a = 7;<br>    increment(&amp;a);<br>    printf(&quot;%d\n&quot;, a);<br>    return 0;<br>}</pre><h3>Question 4</h3><p>What will be printed?</p><pre>#include&lt;stdio.h&gt;<br><br>void modify(int *p)<br>{<br>    *p = *p + 10;<br>}<br>int main()<br>{<br>    int a = 5;<br>    modify(&amp;a);<br>    printf(&quot;%d\n&quot;, a);<br>    return 0;<br>}</pre><h3>Question 5</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>void test(int *p)<br>{<br>    *p = *p * 2;<br>}<br>int main()<br>{<br>    int x = 4;<br>    test(&amp;x);<br>    printf(&quot;%d\n&quot;, x);<br>    return 0;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0f566d209c9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DSA in C- Part5: Double Pointers in C — Understanding Pointers to Pointers]]></title>
            <link>https://medium.com/@suryabhagavanchakkapalli/double-pointers-in-c-understanding-pointers-to-pointers-a35bdca25fa3?source=rss-b7f76858627c------2</link>
            <guid isPermaLink="false">https://medium.com/p/a35bdca25fa3</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[pointers-in-c]]></category>
            <category><![CDATA[dsa-training]]></category>
            <category><![CDATA[double-pointer-in-c]]></category>
            <category><![CDATA[c-programming-language]]></category>
            <dc:creator><![CDATA[Bhagavan]]></dc:creator>
            <pubDate>Tue, 10 Mar 2026 10:31:55 GMT</pubDate>
            <atom:updated>2026-03-10T10:36:35.696Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In previous articles, we explored:</p><ul><li>Memory layout of a C program</li><li>pointers</li><li>pointer arithmetic</li><li>relationship between pointers and arrays</li></ul><p>At this point, we already know that a <strong>pointer stores the memory address of another variable</strong>.</p><p>However, C allows something even more powerful:</p><blockquote><em>A pointer can also store the address of another pointer.</em></blockquote><p>This concept is called a <strong>double pointer</strong> or <strong>pointer to pointer</strong>.</p><p>Double pointers are widely used in real-world C programming. They are essential in many situations, such as:</p><ul><li>modifying pointers inside functions</li><li>dynamic memory allocation of complex structures</li><li>implementing data structures like linked lists and trees</li><li>handling arrays of pointers</li><li>working with command line arguments</li></ul><p>Understanding double pointers properly will strengthen your understanding of <strong>how memory and pointers interact</strong>.</p><h3>What is a Double Pointer</h3><p>A <strong>double pointer</strong> is a pointer that stores the address of another pointer.</p><p>Normal pointer:</p><pre>variable → pointer</pre><p>Double pointer:</p><pre>variable → pointer → pointer</pre><p>Declaration syntax:</p><pre>datatype **pointer_name;</pre><p>Example:</p><pre>int **pp;</pre><p>Here:</p><ul><li>pp is a pointer to a pointer to an integer.</li></ul><h3>Basic Example</h3><p>Let us start with a simple program.</p><pre>#include&lt;stdio.h&gt;<br><br>int main(){<br>    int a=10;<br>    int*p=&amp;a;<br>    int**pp=&amp;p;<br>    printf(&quot;%d\n&quot;,a);<br>    printf(&quot;%p\n&quot;,&amp;a);<br>    printf(&quot;%p\n&quot;,p);<br>    printf(&quot;%p\n&quot;,&amp;p);<br>    printf(&quot;%p\n&quot;,pp);<br>    printf(&quot;%d\n&quot;,*p);<br>    printf(&quot;%d\n&quot;,**pp);<br>    return 0;<br>}</pre><p>Possible output:</p><pre>10<br>0x7ffe100<br>0x7ffe100<br>0x7ffe200<br>0x7ffe200<br>10<br>10</pre><p>Explanation:</p><pre>a → stores value 10<br>p → stores address of a<br>pp → stores address of p</pre><p>So the chain becomes:</p><pre>pp → p → a</pre><h3>Understanding Dereferencing Levels</h3><p>Each * dereferences one level.</p><p>Example variables:</p><pre>int a=10;<br>int*p=&amp;a;<br>int**pp=&amp;p;</pre><p>Meaning:</p><pre>p stores address of a<br>pp stores address of p</pre><p>Dereferencing:</p><pre>*p → value of a<br>**pp → value of a</pre><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main(){<br>    int a=20;<br>    int*p=&amp;a;<br>    int**pp=&amp;p;<br>    printf(&quot;%d\n&quot;,*p);<br>    printf(&quot;%d\n&quot;,**pp);<br>    return 0;<br>}</pre><p>Output:</p><pre>20<br>20</pre><p>Both produce the same value because they eventually reach the variable a.</p><h3>Memory Representation</h3><p>Consider the following program.</p><pre>int a=10;<br>int*p=&amp;a;<br>int**pp=&amp;p;</pre><p>Conceptual memory layout:</p><pre>Address      Value<br>1000         10      (a)<br>2000         1000    (p)<br>3000         2000    (pp)</pre><p>Explanation:</p><pre>p stores 1000<br>pp stores 2000</pre><p>Dereferencing:</p><pre>*p → 10<br>**pp → 10</pre><h3>Modifying Value Using Double Pointer</h3><p>A double pointer can also modify the original variable.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main(){<br>    int a=5;<br>    int*p=&amp;a;<br>    int**pp=&amp;p;<br>    **pp=50;<br>    printf(&quot;%d\n&quot;,a);<br>    return 0;<br>}</pre><p>Output:</p><pre>50</pre><p>Explanation:</p><pre>**pp refers to variable a</pre><p>So modifying **pp changes the value of a.</p><h3>Why Double Pointers Are Needed</h3><p>One of the most important uses of double pointers occurs when we want to <strong>modify a pointer inside a function</strong>.</p><p>Remember:</p><p>In C, function arguments are passed <strong>by value</strong>.</p><p>This means a function receives a <strong>copy of the variable</strong>, not the original variable.</p><p>If we pass a pointer to a function, the function receives a <strong>copy of the pointer</strong>.</p><p>Therefore, modifying that pointer inside the function will not affect the original pointer.</p><p>To modify the original pointer, we must pass <strong>the address of the pointer</strong>, which means we must use a <strong>double pointer</strong>.</p><h3>Example — Problem Without a Double Pointer</h3><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>void allocate(int*p){<br>    p=malloc(sizeof(int));<br>    *p=10;<br>}<br><br>int main(){<br>    int*p=NULL;<br>    allocate(p);<br>    <br>    if(p==NULL)<br>       printf(&quot;Pointer not modified\n&quot;);<br>    return 0;<br>}</pre><p>Output:</p><pre>Pointer not modified</pre><p>Explanation:</p><pre>p inside function is only a copy</pre><p>The original pointer remains unchanged.</p><h3>Example — Using a Double Pointer</h3><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>void allocate(int**p){<br>    *p=malloc(sizeof(int));<br>    **p=10;<br>}<br>int main(){<br>    int*p=NULL;<br>    allocate(&amp;p);<br>    printf(&quot;%d\n&quot;,*p);<br>    free(p);<br>    return 0;<br>}</pre><p>Output:</p><pre>10</pre><p>Explanation:</p><pre>&amp;p → address of pointer p</pre><p>The function modifies the original pointer.</p><h3>Double Pointers with Arrays of Pointers</h3><p>Double pointers are commonly used with arrays of pointers.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main(){<br>    char*names[3]={&quot;Alice&quot;,&quot;Bob&quot;,&quot;Charlie&quot;};<br>    char**p=names;<br>    <br>    printf(&quot;%s\n&quot;,*p);<br>    printf(&quot;%s\n&quot;,*(p+1));<br>    printf(&quot;%s\n&quot;,*(p+2));<br>    return 0;<br>}</pre><p>Output:</p><pre>Alice<br>Bob<br>Charlie</pre><p>Explanation:</p><pre>names → array of pointers<br>p → pointer to first element</pre><p>This is widely used in string handling.</p><h3>Double Pointers and Command Line Arguments</h3><p>The main function in C can receive command-line arguments.</p><p>Example:</p><pre>int main(int argc,char*argv[])</pre><p>This is equivalent to:</p><pre>int main(int argc,char**argv)</pre><p>Here:</p><pre>argv → pointer to pointer to char</pre><p>Each element of argv points to a string.</p><p>Example:</p><pre>#include&lt;stdio.h&gt;<br><br>int main(int argc,char**argv){<br>    for(int i=0;i&lt;argc;i++){<br>        printf(&quot;%s\n&quot;,argv[i]);<br>    }<br>    return 0;<br>}</pre><h3>Interview Practice Questions</h3><h3>Question 1</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>int main(){<br>    int a=5;<br>    int*p=&amp;a;<br>    int**pp=&amp;p;<br>    printf(&quot;%d\n&quot;,**pp);<br>    return 0;<br>}</pre><h3>Question 2</h3><p>What will be printed?</p><pre>#include&lt;stdio.h&gt;<br><br>int main(){<br>    int a=10;<br>    int*p=&amp;a;<br>    int**pp=&amp;p;<br>    *p=20;<br>    printf(&quot;%d\n&quot;,**pp);<br>    return 0;<br>}</pre><h3>Question 3</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>int main(){<br>    int a=15;<br>    int*p=&amp;a;<br>    int**pp=&amp;p;<br>    **pp=30;<br>    printf(&quot;%d\n&quot;,a);<br>    return 0;<br>}</pre><h3>Question 4</h3><p>What will be printed?</p><pre>#include&lt;stdio.h&gt;<br><br>int main(){<br>    int a=7;<br>    int*p=&amp;a;<br>    int**pp=&amp;p;<br>    printf(&quot;%p\n&quot;,pp);<br>    printf(&quot;%p\n&quot;,&amp;p);<br>    return 0;<br>}</pre><h3>Question 5</h3><p>Predict the output.</p><pre>#include&lt;stdio.h&gt;<br><br>int main(){<br>    int a=5;<br>    int*p=&amp;a;<br>    int**pp=&amp;p;<br>    printf(&quot;%d\n&quot;,*p+5);<br>    return 0;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a35bdca25fa3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>