String swapping problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anonymous@coolgroups.com

    #1

    String swapping problem

    Hi CLCers,
    I tried the following code for swapping a string, but it is
    not working. Inside the swap function the strings are
    printed correctly, but when back in main() the strings are
    not swapped at all. Thanks in advance. Here is the code.

    #include<stdio. h>
    int main()
    {
    void swap(char * string1, char * string2);

    char * string1 = "Hello World";
    char * string2 = "Hello Jupiter";
    swap(string1, string2);
    printf("%s\n%s\ n",string1,stri ng2);
    return 0;
    }


    void swap( char * string1, char * string2)
    {
    char * temp;
    temp = string1;
    string1 = string2;
    string2 = temp;
    printf("%s\n%s\ n",string1,stri ng2);
    }


    Sha
  • Régis Troadec

    #2
    Re: String swapping problem


    <anonymous@cool groups.com> a écrit dans le message de
    news:540ca0d062 ec7beb092dd942c d5eee81@news.sc biz.com...[color=blue]
    > Hi CLCers,[/color]

    Hi,
    [color=blue]
    > I tried the following code for swapping a string, but it is
    > not working. Inside the swap function the strings are
    > printed correctly, but when back in main() the strings are
    > not swapped at all. Thanks in advance. Here is the code.
    >
    > #include<stdio. h>
    > int main()
    > {
    > void swap(char * string1, char * string2);
    >
    > char * string1 = "Hello World";
    > char * string2 = "Hello Jupiter";
    > swap(string1, string2);
    > printf("%s\n%s\ n",string1,stri ng2);[/color]

    The strings string1 and string2 you're printing here are those which are in
    the main() scope.
    [color=blue]
    > return 0;
    > }
    >
    >
    > void swap( char * string1, char * string2)
    > {
    > char * temp;
    > temp = string1;
    > string1 = string2;
    > string2 = temp;
    > printf("%s\n%s\ n",string1,stri ng2);[/color]

    The strings string1 and string2 you're printing here are those which are in
    the swap() scope (local copies of the string1 and string2 pointers).

    Regis


    Comment

    • Case

      #3
      Re: String swapping problem

      anonymous@coolg roups.com wrote:[color=blue]
      > Hi CLCers,
      > I tried the following code for swapping a string, but it is
      > not working. Inside the swap function the strings are
      > printed correctly, but when back in main() the strings are
      > not swapped at all. Thanks in advance. Here is the code.
      >
      > #include<stdio. h>
      > int main()
      > {
      > void swap(char * string1, char * string2);
      >
      > char * string1 = "Hello World";
      > char * string2 = "Hello Jupiter";
      > swap(string1, string2);
      > printf("%s\n%s\ n",string1,stri ng2);
      > return 0;
      > }
      >
      >
      > void swap( char * string1, char * string2)
      > {
      > char * temp;
      > temp = string1;
      > string1 = string2;
      > string2 = temp;
      > printf("%s\n%s\ n",string1,stri ng2);
      > }[/color]

      In swap() string1 and string2 are local copies of the addresses
      of the two strings. So, swap() simply swaps the value of these
      local copies (which are addresses); the strings themselves are
      not modified at all.

      In C function parameters are passed by value and not by reference.

      This function would work if you'd pass the address of string1 and
      string2 in main(). void swap(char **string1, char **string2) ...
      Called with swap(&string1, &string2).

      Kees[color=blue]
      >
      >
      > Sha[/color]

      Comment

      • Lew Pitcher

        #4
        Re: String swapping problem

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        anonymous@coolg roups.com wrote:[color=blue]
        > Hi CLCers,
        > I tried the following code for swapping a string, but it is
        > not working. Inside the swap function the strings are
        > printed correctly, but when back in main() the strings are
        > not swapped at all. Thanks in advance. Here is the code.[/color]

        See my comments below...
        [color=blue]
        > #include<stdio. h>
        > int main()
        > {
        > void swap(char * string1, char * string2);
        >
        > char * string1 = "Hello World";
        > char * string2 = "Hello Jupiter";[/color]

        You have two pointers (string1 and string2), each pointing to something that
        cannot be altered. This probably isn't what you want, but it's what you asked for.
        [color=blue]
        > swap(string1, string2);[/color]

        In C, all function calls are 'call-by-value'.
        This invocation of swap() passes the swap() function a /copy/ of the string1
        pointer, and a /copy/ of the string2 pointer. Neither the string1 nor the
        string2 pointer are passed directly to the swap() function, and swap() will not
        affect either pointer.
        [color=blue]
        > printf("%s\n%s\ n",string1,stri ng2);[/color]

        string1, not having been changed since it's inception, still points at the
        unalterable string "Hello World". Likewise, string2 still points at the
        unalterable string "Hello Jupiter". And that's what you get from this printf().
        [color=blue]
        > return 0;
        > }
        >
        >
        > void swap( char * string1, char * string2)
        > {[/color]

        swap() is acting on local copies of the original string1 and string2 pointers.
        It doesn't do anything to the original pointers.
        [color=blue]
        > char * temp;
        > temp = string1;
        > string1 = string2;
        > string2 = temp;[/color]

        So, you've swapped your local copies around, so that your local variable called
        string1 now contains the pointer originally obtained in your local string2
        variable, and string2 now contains the pointer originally obtained in your local
        string1 variable.
        [color=blue]
        > printf("%s\n%s\ n",string1,stri ng2);[/color]

        And you print the strings pointed to by string1 (now containing a pointer to
        "Hello Jupiter") and string2 (now containing a pointer to "Hello World").

        You haven't changed the original pointers (declared in main()).
        [color=blue]
        > }
        >
        >
        > Sha[/color]


        - --
        Lew Pitcher
        IT Consultant, Enterprise Application Architecture,
        Enterprise Technology Solutions, TD Bank Financial Group

        (Opinions expressed are my own, not my employers')
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.4 (MingW32)

        iD8DBQFAqLVVagV FX4UWr64RAhFaAK DQBtWEEJ/Tg3oFMH0/2XntvIkwSACg9ZC a
        fuACUoGcxtM7UHc V/HrhNpU=
        =94pq
        -----END PGP SIGNATURE-----

        Comment

        • Rob van der Leek

          #5
          Re: String swapping problem

          In article <540ca0d062ec7b eb092dd942cd5ee e81@news.scbiz. com>,
          anonymous@coolg roups.com wrote:[color=blue]
          > Hi CLCers,
          > I tried the following code for swapping a string, but it is not
          > working. Inside the swap function the strings are printed correctly,
          > but when back in main() the strings are not swapped at all. Thanks in
          > advance. Here is the code.
          >
          > int main()
          > {
          > void swap(char * string1, char * string2);
          >
          > char * string1 = "Hello World";
          > char * string2 = "Hello Jupiter";
          > swap(string1, string2);
          > printf("%s\n%s\ n",string1,stri ng2);
          > return 0;
          > }
          >
          >
          > void swap( char * string1, char * string2)
          > {
          > char * temp;
          > temp = string1;
          > string1 = string2;
          > string2 = temp;
          > printf("%s\n%s\ n",string1,stri ng2);
          > }[/color]

          In C parameters are passed by value, this also holds for pointers. This
          means that string1 inside swap is a copy of string1 in the scope of
          main. To modify the value of main's string1 (and string2) inside swap
          you must pass it as a reference from main to swap, e.g.:

          void swap (char **string1, char **string2)
          {
          char *temp;
          temp = *string1;
          *string1 = *string2;
          *string2 = temp;
          }

          and call it from main() like:

          swap(&string1, &string2);

          Also, don't forget to include "stdio.h" since you're using printf().

          Regards,
          --
          Rob van der Leek | rob(at)ricardis (dot)tudelft(do t)nl
          Ricardishof 73-A | http://www.ricardis.tudelft.nl/~rob
          2614 JE Delft, The Netherlands
          +31 (0)6 155 244 60

          Comment

          • A

            #6
            Re: String swapping problem

            Here's the code

            #include <stdio.h>

            void swap(char *&st1, char *&st2);

            int main() {

            char *string1 = "Hello World";

            char *string2 = "Hello Jupiter";

            swap(string1, string2);

            printf("%s\n%s\ n",string1,stri ng2);

            return 0;

            }

            void swap(char* &st1, char* &st2) {

            char* tmp;

            tmp = st1;

            st1= st2;;

            st2 = tmp;

            }


            Comment

            • Régis Troadec

              #7
              Re: String swapping problem


              "A" <fercavassa@ali ceposta.com> a écrit dans le message de
              news:6s5qc.2756 70$rM4.11720969 @news4.tin.it.. .

              Hi,
              [color=blue]
              > Here's the code
              >
              > #include <stdio.h>
              >
              > void swap(char *&st1, char *&st2);[/color]

              No, that's C++ stuff. There are no references in C.

              [snipped]

              Regis


              Comment

              • A

                #8
                Re: String swapping problem

                Sorry I didn't know


                Comment

                Working...