Merge Two Arrays in C++
Join two fixed arrays {1,2,3} and {4,5,6} end to end in C++ with two copy loops. A short drill on the n1 + i offset, with no input to get in the way.
1#include <iostream>2using namespace std;34int main() {5 int arr1[] = {1, 2, 3};6 int arr2[] = {4, 5, 6};7 int n1 = 3, n2 = 3;89 int merged[n1 + n2];1011 // Copy first array12 for (int i = 0; i < n1; i++) {13 merged[i] = arr1[i];14 }1516 // Copy second array17 for (int i = 0; i < n2; i++) {18 merged[n1 + i] = arr2[i];19 }2021 cout << "Merged array: ";22 for (int i = 0; i < n1 + n2; i++) {23 cout << merged[i] << " ";24 }25 cout << endl;2627 return 0;28}
Sample Output
Merged array: 1 2 3 4 5 6
arr1 followed by arr2.
In this one
- How to compute array sizes safely with sizeof
- How the n1 + i offset appends the second array
- Why const sizes make the merged array standard C++
- How concatenation differs from a sorted merge
How it works
- 1
Define arr1 and arr2 with three values each.
- 2
Create merged with room for six.
- 3
Copy arr1 into slots 0–2.
- 4
Copy arr2 into slots 3–5.
- 5
Print the merged array.
Edge cases
Sizes out of sync
Add a 4th value to arr1 but leave n1 = 3.
The extra value is never copied. Use sizeof(arr1) / sizeof(arr1[0]) to keep the size correct.
Unsorted inputs
Use {5, 1} and {3, 2}.
The output is 5 1 3 2. This program only joins; it does not order.
What's going on
This is the no-input version of merging: both arrays are written into the code, so the only thing to watch is where each element lands.
int arr1[] = {1, 2, 3}; and int arr2[] = {4, 5, 6};
The empty brackets let the compiler count the elements from the braces. Both have 3.
int n1 = 3, n2 = 3;
The sizes are typed by hand. If you later add a fourth number to arr1 and forget to update n1, the copy loop silently leaves it out. sizeof(arr1) / sizeof(arr1[0]) computes the count for you and avoids that mismatch.
int merged[n1 + n2];
Because n1 and n2 are ordinary variables, not const, this is technically a run-time sized array, which is a compiler extension in C++. Declaring them const int makes it standard. It holds 6 values.
First loop: merged[i] = arr1[i]; for i = 0, 1, 2. The first three slots get 1, 2, 3.
Second loop: merged[n1 + i] = arr2[i]; for i = 0, 1, 2. The target index starts at 3, so 4, 5, 6 go into slots 3, 4, 5. The n1 + is the offset that skips over the part already filled.
Final loop prints all six with spaces: 1 2 3 4 5 6.
What this does not do: sort. Change arr2 to {0, 9, 2} and the result is 1 2 3 0 9 2. If both arrays are already sorted and you want one sorted result, the two-pointer merge on Merge Two Sorted Arrays is the right tool.
In modern C++ you would usually reach for vector: vector<int> m(a); m.insert(m.end(), b.begin(), b.end());. The loops here are what that call does underneath.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Where does arr2[2] land?
Pick an answer to check it.
Question 2
What does sizeof(arr1) / sizeof(arr1[0]) give for arr1?
Pick an answer to check it.
Questions people ask
How is this different from Merge Arrays in the array topic?
That version reads sizes and values from the keyboard. This one hardcodes them so the indexing is the only focus.
Can I merge more than two arrays?
Yes. Keep a running offset and add each array's length after copying it.
What does std::copy do here?
copy(arr2, arr2 + n2, merged + n1) does the second loop in one call.