swapping within a char pointer array

This is a discussion on swapping within a char pointer array within the C++ Programming forums, part of the General Programming Boards category; While reading about char pointer arrays, I was told that these offer, among other things, the advantage of swapping 2 ...

  1. #1
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317

    swapping within a char pointer array

    While reading about char pointer arrays, I was told that these offer, among other things, the advantage of swapping 2 positions without the need to store one of them in a temporary location just like we have to do with normal arrays.

    It gives no example as to how exactly one does it and I couldn't code it. Please help me out with the following code:
    Code:
    char* [] pstr = { "string1", "string2", "string3", "string4" };
    How do I go about swapping say, pstr[2] contents with pstr[1]?
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > pstr[2] contents with pstr[1]?

    char *temp = pstr[1];
    pstr[1] = pstr[2];
    pstr[2] = temp;

    Tada!

    I assume that if it was an array of int's, you wouldn't have had any problem?

  3. #3
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Hmm... that's exactly how I swap array elements in normal arrays (except of course that here I have a pointer as a temp variable).

    It's obvious I misinterpret the text... at some point it reads:

    From Wrox Press C++ Tutorials - Ivor Horton
    If we had stored these simply as strings, as we did in Ex3_04.cpp, a great deal of copying would be necessary - we would need to copy the whole string 'Robert Redford' to a temporary location while we copied 'Oliver Hardy' in its place, and then we would need to copy 'Robert Redford' to the end position. This would require significantly more computer time to execute.
    The Ex3_04.cpp he refers to is an array of strings.

    (gosh! damn, pointers! )
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    From Wrox Press C++ Tutorials - Ivor Horton
    If we had stored these simply as strings, as we did in Ex3_04.cpp, a great deal of copying would be necessary - we would need to copy the whole string 'Robert Redford' to a temporary location while we copied 'Oliver Hardy' in its place, and then we would need to copy 'Robert Redford' to the end position. This would require significantly more computer time to execute.
    Not quite. std::string defines the member function swap, which has constant-time complexity. So:
    Code:
    std::string array[] = { "string1", "string2", "string3", "string4" };
    array[1].swap(array[2]); // constant-time
    std::swap(array[1], array[2]); // also constant-time, if not - complain to library vendor
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-13-2009, 11:03 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21