Passing the value by reference is same as pointer by reference

Discussion in 'C++' started by sam pal, Jul 16, 2003.

  1. sam pal

    sam pal Guest

    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,"Hello");
    }
    int main(){

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

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

    *i1=0;
    display2(i1);
    cout << *i1 << endl;
    }
     
    sam pal, Jul 16, 2003
    #1
    1. Advertising

  2. sam pal

    Artie Gold Guest

    sam pal wrote:
    > 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);
    > }


    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.

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


    Here, you're changing what pchr points to, as opposed to pchr itself.

    > int main(){
    >
    > int i =0;
    > int *i1=new int;
    > char *str = new char [20];
    >
    > display(i);
    > cout << i << endl;
    > strcpy(str,"Temp");
    > display1(str);
    > cout <<"Value="<< str << endl;
    >
    > *i1=0;
    > display2(i1);
    > cout << *i1 << endl;
    > }


    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
     
    Artie Gold, Jul 16, 2003
    #2
    1. Advertising

  3. sam pal

    Ron Natalie Guest

    "sam pal" <> wrote in message news:...
    > I have the following programs and please clear my doubts. Passing the
    > value by reference is same as pointer by reference. Is this true?
    >

    No. And that's not what you are doing.

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


    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.
     
    Ron Natalie, Jul 16, 2003
    #3
  4. sam pal wrote:

    > 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 display0(int i){ // Pass by value
    ++i;
    > }
    >
    > 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");
    > }


    void display3(char& c){ // Pass by reference
    std::cout << &c << std::endl;
    strcpy(&c, "Hello");
    }

    > 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;
    > }
     
    E. Robert Tisdale, Jul 16, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JW
    Replies:
    1
    Views:
    450
    David Jeske
    Jul 12, 2003
  2. J W
    Replies:
    0
    Views:
    391
  3. jimjim
    Replies:
    16
    Views:
    844
    Jordan Abel
    Mar 28, 2006
  4. Hicham Mouline
    Replies:
    100
    Views:
    2,088
    Noah Roberts
    Aug 25, 2009
  5. A
    Replies:
    7
    Views:
    639
Loading...

Share This Page