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!";
destis an array of 20 bytes on the stack, initialized with 7 characters plus a null terminator.str2points to the string literal"world!"in read-only memory.
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;
pstarts at the very beginning of thedestarray.
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.
*pis ‘H’, ’e’, ’l’, ’l’, ‘o’, ‘,’, ’ ’ ->pkeeps advancing.*pis'\0'-> The loop stops.
dest: [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | '\0' | | | ... ]
^
p
Step 3: Initialize q
char *q = str2;
qis created and points to the start ofstr2.
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:
*qis'w'.*p = *q;-> Overwrites\0with'w'.p++; q++;-> Both advance.
dest: [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | | | ... ]
^
p
str2: [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
^
q
Iteration 2:
*qis'o'.*p = *q;-> Copies'o'.p++; q++;
dest: [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | | ... ]
^
p
str2: [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
^
q
Iteration 3:
*qis'r'.*p = *q;-> Copies'r'.p++; q++;
dest: [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | 'r' | | ... ]
^
p
str2: [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
^
q
Iteration 4:
*qis'l'.*p = *q;-> Copies'l'.p++; q++;
dest: [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | 'r' | 'l' | | ... ]
^
p
str2: [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
^
q
Iteration 5:
*qis'd'.*p = *q;-> Copies'd'.p++; q++;
dest: [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | | ... ]
^
p
str2: [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
^
q
Iteration 6:
*qis'!'.*p = *q;-> Copies'!'.p++; q++;
dest: [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | | ... ]
^
p
str2: [ 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' ]
^
q
Loop Exit:
- The loop condition checks
*q. Since*qis now'\0', the loop finishes.
Step 5: Seal Off the String
*p = '\0';
pcurrently points to the empty slot right after the exclamation mark. This line places the critical null terminator there.
dest: [ 'H' | 'e' | 'l' | 'l' | 'o' | ',' | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | '\0' | ... ]
^
p
Step 6: Print Result
printf("Concatenated string: %s\n", dest);
- Output:
Concatenated string: Hello, world!