The Wayback Machine - https://web.archive.org/web/20240118175251/https://www.geeksforgeeks.org/how-to-concatenate-two-integer-arrays-without-using-loop-in-c/
Open In App
Related Articles

How to concatenate two integer arrays without using loop in C ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given two arrays such that first array has enough extra space to accommodate elements of second array. How to concatenate second array to first in C without using any loop in program?

Example:

Input: arr1[5] = {1, 2, 3}
       arr2[]  = {4, 5}
Output: arr1[] = {1, 2, 3, 4, 5}

We strongly recommend you to minimize your browser and try this yourself first.

 

Hint: We may use library functions in C.

The idea is to use memcpy() or memmove() in C.




// arr1[] is of size m+n and arr2[] is of size n. This function
// appends contents of arr2[] at the end of arr1[]
void concatenate(int arr1[], int arr2[], int m, int n)
{
   memcpy(arr1 + m, arr2, sizeof(arr2)); 
}


See this for complete running code.

Thanks to Utkarsh Trivedi for suggesting above solution.

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 15 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads