char*& or char*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pembed2003

    char*& or char*

    Hi coders,
    I have the following:

    void f1(char* &s){
    *s = 'a';
    }

    void f2(char* s){
    *s = 'b';
    }

    int main(int argc,char** argv){
    char a[] = "1234";
    char* b = a;
    printf("%s\n",a );
    f1(b);
    printf("%s\n",a );
    f2(b);
    printf("%s\n",a );
    return 0;
    }

    The above prints:

    1234
    a234
    b234

    My question is:

    1. How is char*& different from char* since they both do the same thing?
    2. When do you use the first form and the second form?

    Thanks!
  • Thomas Stegen

    #2
    Re: char*& or char*

    pembed2003 wrote:

    I have modified your program, spot the difference in the output.

    void f1(char* &s){
    s = "abcd";
    }

    void f2(char* s){
    s = "efgh";
    }

    int main(int argc,char** argv){
    char a[] = "1234";
    char* b = a;
    printf("%s\n",a );
    f1(b);
    printf("%s\n",a );
    f2(b);
    printf("%s\n",a );
    return 0;
    }

    --
    Thomas.


    Comment

    • Rolf Magnus

      #3
      Re: char*& or char*

      pembed2003 wrote:
      [color=blue]
      > Hi coders,
      > I have the following:
      >
      > void f1(char* &s){
      > *s = 'a';
      > }
      >
      > void f2(char* s){
      > *s = 'b';
      > }
      >
      > int main(int argc,char** argv){
      > char a[] = "1234";
      > char* b = a;
      > printf("%s\n",a );
      > f1(b);
      > printf("%s\n",a );
      > f2(b);
      > printf("%s\n",a );
      > return 0;
      > }
      >
      > The above prints:
      >
      > 1234
      > a234
      > b234
      >
      > My question is:
      >
      > 1. How is char*& different from char* since they both do the same
      > thing?[/color]

      The first passes a reference to the pointer to the function, the other
      one a copy of it. So with the first one, you can change the value of
      the original pointer (which your example doesn't do), with the second
      not, because the function is working on a local copy of it.
      [color=blue]
      > 2. When do you use the first form and the second form?[/color]

      You use the first form if you want to modify the object from the
      function. And you use const references if you don't want to modify the
      object, but want to avoid the overhead of copying it. The second form
      is mostly used with built-int types (for which copying doesn't impose
      an overhead) if you don't want to modify the original object. Also, the
      second form allows to pass temporaries:

      void foo(int i);
      void bar(int& j);

      int main()
      {
      foo(3); //ok
      bar(3); //not possible
      }

      Comment

      • Ioannis Vranos

        #4
        Re: char*& or char*

        "pembed2003 " <pembed2003@yah oo.com> wrote in message
        news:db593abd.0 404150943.46dc7 67f@posting.goo gle.com...[color=blue]
        > Hi coders,
        > I have the following:
        >
        > void f1(char* &s){
        > *s = 'a';
        > }
        >
        > void f2(char* s){
        > *s = 'b';
        > }
        >
        > int main(int argc,char** argv){
        > char a[] = "1234";
        > char* b = a;
        > printf("%s\n",a );
        > f1(b);
        > printf("%s\n",a );
        > f2(b);
        > printf("%s\n",a );
        > return 0;
        > }
        >
        > The above prints:
        >
        > 1234
        > a234
        > b234
        >
        > My question is:
        >
        > 1. How is char*& different from char* since they both do the same thing?
        > 2. When do you use the first form and the second form?[/color]


        With f1() you change the value of the pointed object using the original
        pointer variable itself, while in the second a temporary local pointer
        variable is created with the name s and the same value with the original
        pointer variable.






        Ioannis Vranos

        Comment

        • Rolf Magnus

          #5
          Re: char*&amp; or char*

          Thomas Stegen wrote:
          [color=blue]
          > pembed2003 wrote:
          >
          > I have modified your program, spot the difference in the output.
          >
          > void f1(char* &s){
          > s = "abcd";
          > }
          >
          > void f2(char* s){
          > s = "efgh";
          > }
          >
          > int main(int argc,char** argv){
          > char a[] = "1234";
          > char* b = a;
          > printf("%s\n",a );[/color]

          ITYM:
          printf("%s\n",b );
          [color=blue]
          > f1(b);
          > printf("%s\n",a );[/color]

          printf("%s\n",b );
          [color=blue]
          > f2(b);
          > printf("%s\n",a );[/color]

          printf("%s\n",b );
          [color=blue]
          > return 0;
          > }[/color]


          Comment

          • John Harrison

            #6
            Re: char*&amp; or char*


            "pembed2003 " <pembed2003@yah oo.com> wrote in message
            news:db593abd.0 404150943.46dc7 67f@posting.goo gle.com...[color=blue]
            > Hi coders,
            > I have the following:
            >
            > void f1(char* &s){
            > *s = 'a';
            > }
            >
            > void f2(char* s){
            > *s = 'b';
            > }
            >
            > int main(int argc,char** argv){
            > char a[] = "1234";
            > char* b = a;
            > printf("%s\n",a );
            > f1(b);
            > printf("%s\n",a );
            > f2(b);
            > printf("%s\n",a );
            > return 0;
            > }
            >
            > The above prints:
            >
            > 1234
            > a234
            > b234
            >
            > My question is:
            >
            > 1. How is char*& different from char* since they both do the same thing?[/color]

            They both do the same thing in your code, that doesn't mean they do the same
            thing always.
            [color=blue]
            > 2. When do you use the first form and the second form?[/color]

            When you pass by reference you pass a reference to the original object to
            your function, when you pass by value you get a copy of the original object.
            There are many different reasons for preferring one over the other.

            Here's one example

            void f1(char* &s){
            s = "a";
            }

            void f2(char* s){
            s = "b";
            }

            int main()
            {
            char* s = "c";
            cout << s << '\n';
            f2(s);
            cout << s << '\n';
            f1(s);
            cout << s << '\n';
            }

            Try running that and see what difference the reference makes.

            john


            Comment

            • Thomas Stegen

              #7
              Re: char*&amp; or char*

              Rolf Magnus wrote:
              [color=blue]
              > Thomas Stegen wrote:[/color]
              [snip]

              Aiaiaiai, let this be a lesson for all, don't modify code
              without reading it very very properly.!!!
              (See what I did there)

              --
              Thomas.

              Comment

              • pembed2003

                #8
                Re: char*&amp; or char*

                "John Harrison" <john_andronicu s@hotmail.com> wrote in message news:<c5ml3o$3g 2vb$1@ID-196037.news.uni-berlin.de>...[color=blue]
                > "pembed2003 " <pembed2003@yah oo.com> wrote in message
                > news:db593abd.0 404150943.46dc7 67f@posting.goo gle.com...[color=green]
                > > Hi coders,
                > > I have the following:
                > >
                > > void f1(char* &s){
                > > *s = 'a';
                > > }
                > >
                > > void f2(char* s){
                > > *s = 'b';
                > > }
                > >
                > > int main(int argc,char** argv){
                > > char a[] = "1234";
                > > char* b = a;
                > > printf("%s\n",a );
                > > f1(b);
                > > printf("%s\n",a );
                > > f2(b);
                > > printf("%s\n",a );
                > > return 0;
                > > }
                > >
                > > The above prints:
                > >
                > > 1234
                > > a234
                > > b234
                > >
                > > My question is:
                > >
                > > 1. How is char*& different from char* since they both do the same thing?[/color]
                >
                > They both do the same thing in your code, that doesn't mean they do the same
                > thing always.
                >[color=green]
                > > 2. When do you use the first form and the second form?[/color]
                >
                > When you pass by reference you pass a reference to the original object to
                > your function, when you pass by value you get a copy of the original object.
                > There are many different reasons for preferring one over the other.
                >
                > Here's one example
                >
                > void f1(char* &s){
                > s = "a";
                > }
                >
                > void f2(char* s){
                > s = "b";
                > }
                >
                > int main()
                > {
                > char* s = "c";
                > cout << s << '\n';
                > f2(s);
                > cout << s << '\n';
                > f1(s);
                > cout << s << '\n';
                > }
                >
                > Try running that and see what difference the reference makes.
                >
                > john[/color]

                Good example. I think I understand it. In f2(), s is a local
                automiatic variable, when you say 's = "b";', this local variable is
                modified. In f1(), s is a reference to main's s so when you say 's =
                "a";', main's s is also changed.

                Comment

                • John Harrison

                  #9
                  Re: char*&amp; or char*

                  >[color=blue]
                  > Good example. I think I understand it. In f2(), s is a local
                  > automiatic variable, when you say 's = "b";', this local variable is
                  > modified. In f1(), s is a reference to main's s so when you say 's =
                  > "a";', main's s is also changed.[/color]

                  You got it.

                  john


                  Comment

                  Working...