How to split array into two arrays in C -


say have array in c

int array[6] = {1,2,3,4,5,6} 

how split

{1,2,3} 

and

{4,5,6} 

would possible using memcpy?

thank you,

nonono

sure. straightforward solution allocate 2 new arrays using malloc , using memcpy copy data 2 arrays.

int array[6] = {1,2,3,4,5,6} int *firsthalf = malloc(3 * sizeof(int)); if (!firsthalf) {   /* handle error */ }  int *secondhalf = malloc(3 * sizeof(int)); if (!secondhalf) {   /* handle error */ }  memcpy(firsthalf, array, 3 * sizeof(int)); memcpy(secondhalf, array + 3, 3 * sizeof(int)); 

however, in case original array exists long enough, might not need that. 'split' array 2 new arrays using pointers original array:

int array[6] = {1,2,3,4,5,6} int *firsthalf = array; int *secondhalf = array + 3; 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -