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.
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
Share your thoughts in the comments
Add Your Comment
Please Login to comment...