Swap problem

This is a discussion on Swap problem within the C Programming forums, part of the General Programming Boards category; Hi I had been working on some code and I had to reverse a string. I tried to use the ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    8

    Swap problem

    Hi
    I had been working on some code and I had to reverse a string. I tried to use the swap function as follows:

    Code:
    void swap(char *i, char *j)
    {
      char temp = *i;
      *i = *j;
      *j = c;
    }

    No matter what swapping technique I use, the compiler won't let me do it. I checked the addresses being passed and they were correct. This is what I get.

    Code:
         55 [main] newrev 3716 _cygtls::handle_exceptions: Error while dumping state
     (probably corrupted stack)
    Segmentation fault (core dumped)
    Obviously I dont need to assign memory to the arguments because that would make them point to a new address in memory. What am I missing?

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How are you calling it?
    Code:
    int main( void )
    {
        char a = 'a', b = 'b';
    
        swap( &a, &b );
        printf( "a is %c, and b is %c\n", a, b );
    
        return 0;
    }
    Oh, also you have a 'temp', but you try to use a 'c' variable. I'll assume that was a typo.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Code:
    void swap(char &a, char &b) {
        char temp = a;
        a = b;
        b = temp;
    }
    Up to your tutorial reading skills to figure out how/why this works.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    8
    i m calling them from main.

    Code:
    int main()
    {
     char *i,*j;
     char *str = "something";
      
     //At the point of calling swap below i = str and j is pointing to the last char of str
     //I've checked for this using addresses of str and j.
     
     swap(i,j);
     
     return 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    8
    I know how that works.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Wraithan
    Code:
    void swap(char &a, char &b) {
        char temp = a;
        a = b;
        b = temp;
    }
    Up to your tutorial reading skills to figure out how/why this works.
    Then read up on it again to see why it doesn't work. Here's a hint, this is the C board.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by devilhaunts
    i m calling them from main.

    Code:
    int main()
    {
     char *i,*j;
     char *str = "something";
      
     //At the point of calling swap below i = str and j is pointing to the last char of str
     //I've checked for this using addresses of str and j.
     
     swap(i,j);
     
     return 0;
    }
    If inside a function, you make changes to an argument, and you want those changes to keep once your function exits, you have to have a pointer to that type of object.

    Thus, 'char *' will let you make changes to a char. If you need to make a 'char *' point to something else, then your arguments need to be 'char **'.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    8
    That certainly makes more sense.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    8
    Quote Originally Posted by quzah
    Then read up on it again to see why it doesn't work. Here's a hint, this is the C board.


    Quzah.
    I know, that's how C++ does it. I hope the comment was not directed at me.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    There is an easy solution to this problem. The pointer str points to a string literal, something that you cannot modify only look at and access. What you are doing is comparable to changing the value of the number 4 to 0, or something.

    Anyway, change your pointer to an array, and your problem should be solved. That also works.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    8
    @quzah & citizen

    Both of your ideas work. Thanks.

  12. #12
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Nvm me... I should learn better to hit new posts and reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. array sort output problem
    By radiantarchon28 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2007, 01:49 AM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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