Merging arrays

Arrays and collections / Arrays

//a new array on the heap with room for both; the caller frees it
int *merge(const int a[], size_t aCount, const int b[], size_t bCount) {
    int *result = malloc((aCount + bCount) * sizeof result[0]);
    memcpy(result, a, aCount * sizeof a[0]);
    memcpy(result + aCount, b, bCount * sizeof b[0]);
    return result;
}

void printArray(const char *name, const int arr[], size_t count) {
    printf("%s is {", name);
    for (size_t i = 0; i < count; i++) {
        if (i > 0) {
            printf(", ");
        }
        printf("%d", arr[i]);
    }
    printf("}\n");
}

int firstNumbers[] = {2, 3, 5};
int secondNumbers[] = {7, 11, 13};
size_t firstCount = sizeof firstNumbers / sizeof firstNumbers[0];
size_t secondCount = sizeof secondNumbers / sizeof secondNumbers[0];

int *allNumbers = merge(firstNumbers, firstCount, secondNumbers, secondCount);
//allNumbers is {2, 3, 5, 7, 11, 13}

printArray("allNumbers", allNumbers, firstCount + secondCount);
free(allNumbers);