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

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    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. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    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. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #5
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    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. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    ok, thanks everybody.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote 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.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote 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.

    Quote Originally Posted by oldman
    4. 6 first elements from array[] to array New1[]
    5. And new2[] with 4 last elements from array[]
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I based my answer off of the OP's question. Since h specifically used the [] symbol it ruled out he use of pointers.

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote 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.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM