1、使用额外的数组
#include <stdio.h>
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int mergedArr[]) {
int i, j, k;
// 将第一个数组复制到合并后的数组
for (i = 0; i < size1; i++) {
mergedArr[i] = arr1[i];
}
// 将第二个数组追加到合并后的数组
for (j = 0, k = size1; j < size2; j++, k++) {
mergedArr[k] = arr2[j];
}
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {6, 7, 8, 9, 10};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
int mergedArr[size1 + size2];
mergeArrays(arr1, size1, arr2, size2, mergedArr);
printf("Merged Array: ");
for (int i = 0; i < size1 + size2; i++) {
printf("%d ", mergedArr[i]);
}
return 0;
}
2、使用指针
#include <stdio.h>
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int mergedArr[]) {
int *pMergedArr = mergedArr;
int *pEnd1 = arr1 + size1;
int *pEnd2 = arr2 + size2;
while (arr1 < pEnd1) {
*pMergedArr++ = *arr1++;
}
while (arr2 < pEnd2) {
*pMergedArr++ = *arr2++;
}
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {6, 7, 8, 9, 10};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
int mergedArr[size1 + size2];
mergeArrays(arr1, size1, arr2, size2, mergedArr);
printf("Merged Array: ");
for (int i = 0; i < size1 + size2; i++) {
printf("%d ", mergedArr[i]);
}
return 0;
}
3、使用memcpy函数
#include <stdio.h>
#include <string.h>
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int mergedArr[]) {
memcpy(mergedArr, arr1, size1 * sizeof(int));
memcpy(mergedArr + size1, arr2, size2 * sizeof(int));
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {6, 7, 8, 9, 10};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
int mergedArr[size1 + size2];
mergeArrays(arr1, size1, arr2, size2, mergedArr);
printf("Merged Array: ");
for (int i = 0; i < size1 + size2; i++) {
printf("%d ", mergedArr[i]);
}
return 0;
}
4、使用realloc动态分配内存
#include <stdio.h>
#include <stdlib.h>
int* mergeArrays(int arr1[], int size1, int arr2[], int size2) {
int *mergedArr = (int *)malloc((size1 + size2) * sizeof(int));
if (mergedArr == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(1);
}
for (int i = 0; i < size1; i++) {
mergedArr[i] = arr1[i];
}
for (int i = 0; i < size2; i++) {
mergedArr[size1 + i] = arr2[i];
}
return mergedArr;
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {6, 7, 8, 9, 10};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
int *mergedArr = mergeArrays(arr1, size1, arr2, size2);
printf("Merged Array: ");
for (int i = 0; i < size1 + size2; i++) {
printf("%d ", mergedArr[i]);
}
// Remember to free allocated memory
free(mergedArr);
return 0;
}
5、直接在原数组上合并
#include <stdio.h>
void mergeArrays(int arr1[], int *size1, int arr2[], int size2) {
for (int i = 0; i < size2; i++) {
arr1[*size1 + i] = arr2[i];
}
*size1 += size2;
}
int main() {
int arr1[10] = {1, 2, 3, 4, 5};
int arr2[] = {6, 7, 8, 9, 10};
int size1 = 5;
int size2 = sizeof(arr2) / sizeof(arr2[0]);
mergeArrays(arr1, &size1, arr2, size2);
printf("Merged Array: ");
for (int i = 0; i < size1; i++) {
printf("%d ", arr1[i]);
}
return 0;
}