Amersands and call-by-reference

This is a discussion on Amersands and call-by-reference within the C++ Programming forums, part of the General Programming Boards category; I have the following code: Code: #include <iostream> using namespace std; void change(int in1[]); int main() { int a[5]={1, 1, ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    194

    Amersands and call-by-reference

    I have the following code:
    Code:
    #include <iostream>
    using namespace std;
    
    void change(int in1[]);
    
    int main()
    {
    int a[5]={1, 1, 1, 1, 1}, c;
    
    for(c=0; c<5; c++)
    cout<<a[c]<<endl;
    
    change(a);
    
    for(c=0; c<5; c++)
    cout<<a[c]<<endl;
    }
    
    
    void change(int in1[])
    {
    int count;
    for(count=0; count<5; count++)
    in1[count]*=2;
    }
    This code outputs the following:
    Code:
    1
    1
    1
    1
    1
    2
    2
    2
    2
    2
    I also have this code which is similar:
    Code:
    #include <iostream>
    using namespace std;
    
    void change(int& in1);
    
    int main()
    {
    int a=1;
    
    cout<<a<<endl;
    change(a);
    cout<<a<<endl;
    }
    
    
    void change(int& in1)
    {
    in1*=2;
    }
    It outputs the following:
    Code:
    1
    2
    Why does the second code require an ampersand and the first one not?
    Last edited by thetinman; 12-09-2005 at 10:18 AM.

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    I guess u r asking this>>Why does the second code require an ampersand and the first one not?
    Arrays are passed by address automatically in first code.
    Second code shows one of the two parameter passing techniques in C++...i.e. pass by reference.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Arrays are passed by address automatically in first code.
    ..and int's are passed by value, which means they are copied for the function, so if you change them inside the function, you are changing the copy, and the original int will remain unchanged.

    However, the '&' symbol means you are directing the compiler to pass by reference, which means the parameter name becomes a nickname(or synonym) for the original variable, and if you change the parameter variable, you are actually changing the original variable since both names refer to the same variable.
    Last edited by 7stud; 12-09-2005 at 12:16 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you think about what an array variable is, and what a pointer is and what an int is, it all makes sense. They are all passed to the function in the same way, but because an array variable is just the address of the first entry of the array, when it is passed by value only the address is copied, not the entire array. This allows you to modify the array without adding the & to indicate pass by reference.

Popular pages Recent additions subscribe to a feed

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