# Thread: split the array[] to 2 new arrays new1[], new2[] is this possible ?

1. ## split the array[] to 2 new arrays new1[], new2[] is this possible ?

1. If I have an array with 10 elements array[]
2. And want to split array[] to:
3. 2 new arrays:
4. 6 first elements from array[] to array New1[]
5. And new2[] with 4 last elements from array[]

Is this possible ?

2. Yes and no.
No you can't actually split array[] itself into two variables.
Yes you can create two new variables and copy the contents of array[] into those new variables.

3. can you show some example codes , how to create two new variables and copy the contents of array[] into those new variables ?

I would be grateful. (I have just started with C)

oldman

4. I won't show you how to do it but I'll show you how to initalize an array:
Code:
`int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };`
or

Code:
```int arr[10], c;
for (c=0; c<10; c++)
arr[c] = c + 1;```

5. Well, think about the algorithm you'd use to go about doing it. You want the first 6 elements of one array. How do you normally access an element in an array? How do you set the contents of an array, or the contents of a particular index?

When you've figured that out, the rest is easy.

6. ok, thanks everybody.

7. Originally Posted by Thantos
Yes and no.
No you can't actually split array[] itself into two variables.
Yes you can create two new variables and copy the contents of array[] into those new variables.
You could use two pointers, one pointing at array and one at array+6. This is almost like 2 arrays, just that sizeof() will return the size of the pointers and not the arrays memory usage. At least you don't have to copy any data this way.

8. If you plan on using the two arrays as strings, you couldn't just use two pointers. If you did, you'd have to shift the contents of the "second array" down one, so you could include a null after the 6th character. So no, just using two pointers wouldn't do it.

Quzah.

9. Originally Posted by quzah
If you plan on using the two arrays as strings, you couldn't just use two pointers. If you did, you'd have to shift the contents of the "second array" down one, so you could include a null after the 6th character. So no, just using two pointers wouldn't do it.
Basically what you say is that this method won't work if data needs to be inserted, no matter if it's a terminating \0 or any other data. That's of course true.
Judging by the OP's original post, that didn't seem to be his intention.

Originally Posted by oldman
4. 6 first elements from array[] to array New1[]
5. And new2[] with 4 last elements from array[]

10. I based my answer off of the OP's question. Since h specifically used the [] symbol it ruled out he use of pointers.

11. Originally Posted by Thantos
I based my answer off of the OP's question. Since h specifically used the [] symbol it ruled out he use of pointers.
You could put those behind a pointer, too .
Anyway, looks like he's fine with the copy-solution so it doesn't really matter any more.