To join arrays in C, you can use the memcpy function or manually copy elements from one array to another.
In this example,
arr1
and arr2
.memcpy
function to copy elements of arr2
to the end of arr1
.result
containing elements from both arr1
and arr2
.#include <stdio.h>
#include <string.h>
int main() {
int arr1[] = {1, 2, 3};
int arr2[] = {4, 5, 6};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
int result[size1 + size2];
memcpy(result, arr1, size1 * sizeof(int));
memcpy(result + size1, arr2, size2 * sizeof(int));
for (int i = 0; i < size1 + size2; i++) {
printf("%d ", result[i]);
}
return 0;
}
1 2 3 4 5 6
In this tutorial, we learned How to Join Arrays in C language with well detailed examples.