#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    2

    Short question about swapping strings in C.


    When i define a double array e.g array[5][10], and i use the function swap like this: swap(&array[2], &array[3]).

    Would this be error prone? because i know i could choose to define the following: "char* array[5]" so the array would be an ARRAY OF POINTERS rather than an array of arrays. Is this something that might cause a problem?

    Because i don't see a big difference(?).
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,363
    Rep Power
    1870
    The only difference is the amount of work you need to do.

    With char array[5][10], you would
    - copy up to 10 bytes from array[x] into a temp variable
    - copy array[y] into array[x]
    - copy temp into array[x]
    If you use strcpy, then the obvious problem is how do you allocate the temp array?
    If you instead use a loop and just sway 1 char at a time (temp = array[x][i] ..), you would ideally like to bail out as soon as you'd copied the \0 from both strings, otherwise there would be a penalty for dealing with short strings.

    With char *array[5], everything is so much simpler
    char *temp = array[x];
    array[x] = array[y];
    array[y] = temp;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    2
    And if it's not an array of pointers, but a usual 2-d array, why can't he still swap the addresses?

    Because when i try swapping: "&new_words[j]" and "&new_words[j+1]" inside a loop, while calling to the function swap, the compiler gives the message:

    INCOMPATIBLE POINTER TYPE

    Aren't they still addresses, just like pointers? :chomp:
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,363
    Rep Power
    1870
    > Aren't they still addresses, just like pointers?
    No, an array of pointers (char *array[5]), and an array of arrays (char array[5][10]) are different animals.

    The fact that you can do array[r][c] to access a specific char in both cases doesn't make them completely interchangeable.

    More on arrays and pointers

    Here's something else for you to try as well, printing out the results of
    sizeof(array)
    sizeof(array[0])
    for both styles of array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    2
    I think i'm catching on.

    The sizeof were different as i thought when i printed them.

    Thanks, very informative.

    Comments on this post

    • salem agrees : keep at it - you'll get there :)

IMN logo majestic logo threadwatch logo seochat tools logo