Home

Visualizing String Concatenation in C Step by Step

String concatenation in C doesn’t happen through a magic operator. When you write your own concatenation routine, you are really just shuffling pointers through memory—one byte at a time—until every character from the source has been copied into the destination, followed by a fresh null terminator.

The clearest way to understand this is to slow it down and watch each pointer move. Below is a complete concatenate-style implementation built from two manual while loops, followed by a frame-by-frame walkthrough of exactly what memory looks like at every step.


#include <stdio.h>

int main(void) {
    char dest[20] = "Hello";
    char *str2 = "World";

    // 1. p starts at the beginning of dest
    char *p = dest;

    // 2. Advance p forward until it points to the '\0' of dest
    while (*p != '\0') {
        p++;
    }

    // 3. Create a pointer to read from str2
    char *q = str2;

    // 4. Copy characters from q into p until q hits the null terminator
    while (*q != '\0') {
        *p = *q; // Copy character from str2 into dest
        p++;     // Move dest pointer to next slot
        q++;     // Move str2 pointer to next character
    }

    // 5. Seal off the string by placing the null terminator at the end
    *p = '\0';

    // Print the final result
    printf("Concatenated string: %s\n", dest);

    return 0;
}

Initial State

char dest[20] = "Hello, ";
char *str2 = "world!";
Memory State:
dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | '\0' |   |   | ... (up to 20) ]
         ^
str2:  [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
         ^

Step 1: Initialize p

char *p = dest;
dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | '\0' |   |   | ... ]
         ^
         p

Step 2: Advance p to the Null Terminator

while (*p != '\0') {
    p++;
}

p marches forward until it points to the '\0' character at index 7.

dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | '\0' |   |   | ... ]
                                                   ^
                                                   p

Step 3: Initialize q

char *q = str2;
dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | '\0' |   |   | ... ]
                                                   ^
                                                   p

str2:  [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
         ^
         q

Step 4: Copy Loop (Iteration by Iteration)

while (*q != '\0') {
    *p = *q; // Copy character from str2 into dest
    p++;     // Move dest pointer to next slot
    q++;     // Move str2 pointer to next character
}

Iteration 1:

dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' |   |   | ... ]
                                                         ^
                                                         p

str2:  [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
               ^
               q

Iteration 2:

dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' |   | ... ]
                                                               ^
                                                               p

str2:  [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
                     ^
                     q

Iteration 3:

dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | 'r' |   | ... ]
                                                                     ^
                                                                     p

str2:  [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
                           ^
                           q

Iteration 4:

dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | 'r' | 'l' |   | ... ]
                                                                           ^
                                                                           p

str2:  [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
                                 ^
                                 q

Iteration 5:

dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' |   | ... ]
                                                                                 ^
                                                                                 p

str2:  [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
                                       ^
                                       q

Iteration 6:

dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' |   | ... ]
                                                                                       ^
                                                                                       p

str2:  [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
                                             ^
                                             q

Loop Exit:


Step 5: Seal Off the String

*p = '\0';
dest:  [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' | ... ]
                                                                                       ^
                                                                                       p

Step 6: Print Result

printf("Concatenated string: %s\n", dest);
Tags: CStringPointers