Passing the value by reference is same as pointer by reference

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

    Passing the value by reference is same as pointer by reference

    I have the following programs and please clear my doubts. Passing the
    value by reference is same as pointer by reference. Is this true?

    What is the difference between display and display2?
    How can I change display1 to value by reference if there is
    difference?



    void display(int &i){// Passing value by reference
    ++i;
    }

    void display2(int *i){ // Pointer by reference
    ++(*i);
    }

    void display1(char *pchr){ //I want to change to Value by reference if
    any difference.
    cout << pchr;
    strcpy(pchr,"He llo");
    }
    int main(){

    int i =0;
    int *i1=new int;
    char *str = new char [20];

    display(i);
    cout << i << endl;
    strcpy(str,"Tem p");
    display1(str);
    cout <<"Value="<< str << endl;

    *i1=0;
    display2(i1);
    cout << *i1 << endl;
    }
  • Artie Gold

    #2
    Re: Passing the value by reference is same as pointer by reference

    sam pal wrote:[color=blue]
    > I have the following programs and please clear my doubts. Passing the
    > value by reference is same as pointer by reference. Is this true?
    >
    > What is the difference between display and display2?
    > How can I change display1 to value by reference if there is
    > difference?
    >
    >
    >
    > void display(int &i){// Passing value by reference
    > ++i;
    > }
    >
    > void display2(int *i){ // Pointer by reference
    > ++(*i);
    > }[/color]

    They will have the same effect, iff the argument `i' in display2 points
    to a valid int. The advantage with references is that you don't have to
    worry about a NULL pointer.
    [color=blue]
    >
    > void display1(char *pchr){ //I want to change to Value by reference if
    > any difference.
    > cout << pchr;
    > strcpy(pchr,"He llo");
    > }[/color]

    Here, you're changing what pchr points to, as opposed to pchr itself.
    [color=blue]
    > int main(){
    >
    > int i =0;
    > int *i1=new int;
    > char *str = new char [20];
    >
    > display(i);
    > cout << i << endl;
    > strcpy(str,"Tem p");
    > display1(str);
    > cout <<"Value="<< str << endl;
    >
    > *i1=0;
    > display2(i1);
    > cout << *i1 << endl;
    > }[/color]

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas

    Comment

    • Ron Natalie

      #3
      Re: Passing the value by reference is same as pointer by reference


      "sam pal" <sam_pal@hotmai l.com> wrote in message news:f4121f92.0 307161025.65e61 3f9@posting.goo gle.com...[color=blue]
      > I have the following programs and please clear my doubts. Passing the
      > value by reference is same as pointer by reference. Is this true?
      >[/color]
      No. And that's not what you are doing.
      [color=blue]
      > What is the difference between display and display2?
      > How can I change display1 to value by reference if there is
      > difference?
      >[/color]

      display2 doesn't pass the pointer by reference. It passes a pointer
      by value.

      display1 and display2 are rougly equivelent, other than the vehicle
      for refering to the original object is a pointer in one and a reference
      in the other.

      The practical differences are what you have observed.


      Comment

      • E. Robert Tisdale

        #4
        Re: Passing the value by reference is same as pointer by reference

        sam pal wrote:
        [color=blue]
        > I have the following programs and please clear my doubts.
        > Passing the value by reference is same as pointer by reference.
        > Is this true?[/color]
        [color=blue]
        > What is the difference between display and display2?
        > How can I change display1 to value by reference
        > if there is difference?
        >
        >
        >[/color]
        void display0(int i){ // Pass by value
        ++i;[color=blue]
        > }
        >
        > void display(int& i){ // Pass by reference
        > ++i;
        > }
        >
        > void display2(int* p){ // Pass a reference *through* a pointer
        > ++(*p); // (pointer p is passed by value)
        > }
        >
        > void display1(char* p){
        > //I want to change to Value by reference if any difference.
        > std::cout << p << std::endl;
        > strcpy(p, "Hello");
        > }[/color]

        void display3(char& c){ // Pass by reference
        std::cout << &c << std::endl;
        strcpy(&c, "Hello");
        }
        [color=blue]
        > int main(int argc, char* argv[]){
        >
        > int i = 0;
        > int* i1 = new int;
        > char* str = new char[20];
        >
        > display(i);
        > std::cout << i << std::endl;
        > strcpy(str, "Temp");
        > display1(str);
        > std::cout <<"Value="<< str << std::endl;
        >
        > *i1 = 0;
        > display2(i1);
        > cout << *i1 << endl;
        > }[/color]


        Comment

        Working...