How can I concatenate two arrays in C?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In C programming, concatenating two arrays involves joining them end-to-end to form a new, single array. This process is vital when you need to combine data from two arrays into one, preserving the order of elements. Let's explore how we can achieve array concatenation in C, along with relevant technical explanations and examples.
Understanding Arrays in C
Arrays in C are collections of elements of the same data type, stored in contiguous memory locations. They are fixed in size, meaning you cannot alter their size once they have been created. To concatenate two arrays, you need to create a third array with adequate space to store the elements of both original arrays.
Steps for Concatenating Two Arrays
- Determine the sizes of both arrays.
- Create a new array with a size equal to the sum of the sizes of the two arrays you wish to concatenate.
- Copy elements from the first array into the new array.
- Copy elements from the second array into the new array immediately following the elements of the first array.
Implementation in C
Below is an example to demonstrate how to concatenate two arrays in C:
- In this example, two arrays
array1andarray2are concatenated. - A new integer array
resultis declared with a size equal to the sum of the sizes ofarray1andarray2. - The function
concatenateArraysutilizes twoforloops:- The first loop copies elements from
array1. - The second loop appends elements from
array2, starting from wherearray1ended.
- The concatenated result is then printed to the console.
- Memory Allocation: The new array must be large enough to hold all elements from the two input arrays. Memory allocation with
malloccan be used if you need the array to be dynamic. - Time Complexity: Concatenating arrays is an operation, where
nis the size of the first array andmis the size of the second array. This is due to the necessity of copying each element.
Related reading
- How can I concatenate two arrays in Java?
- How can I configure the heap size when starting a Spring Boot application with embedded Tomcat?
- How can I construct a tree using d3 and its force layout?
- How can I convert a byte array to hexadecimal in Java?
- How can I distinguish between high- and low-performance cores/threads in C?
- How can I fix my C code that determines whether a number is prime?
- How can I convert a comma-separated string to an array?
- How can I convert a dictionary into a list of tuples?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.