Of course. The direct solution is to allocate two new arrays using malloc and then using memcpy to copy the data into two arrays.
int array[6] = {1,2,3,4,5,6} int *firstHalf = malloc(3 * sizeof(int)); if (!firstHalf) { } int *secondHalf = malloc(3 * sizeof(int)); if (!secondHalf) { } memcpy(firstHalf, array, 3 * sizeof(int)); memcpy(secondHalf, array + 3, 3 * sizeof(int));
However, if the original array exists long enough, you may not need to do this. You can simply βsplitβ the array into two new arrays using the pointers in the original array:
int array[6] = {1,2,3,4,5,6} int *firstHalf = array; int *secondHalf = array + 3;
source share