passing an array by pointer or refrence?
I''m trying to clear the pass by value or reference question
Here I take an integer ''a'' and pass it to 2 functions. One by value and the other a reference. See how a reference can be used to change the original variable. Value only sends the value, literally; a copy of the variable.
#include <iostream.h>
void pass_by_reference(int *a);
void pass_by_value(int a);
main(void)
{
int a;
cout << "Enter ''a'' value\n";
cin >> a; //gets input from keyboard
cout << "Value entered:" << a << "\n"; // prints the integer
pass_by_value(a); //sends a copy of the value of ''a''
cout << "Pass by value:" << a << "\n"; // after the function exe
pass_by_reference(&a);
//sends the address of ''a'', so a''s value can be changed
cout << "Pass by reference:" << a << "\n";
// prints a''s new value changed by the function i.e. 13 here
return 0;
}
void pass_by_value(int a)
{
a = 13;
// a is local to this function so ''a'' in the main() doesn''t change
}
void pass_by_reference(int *a)
{
*a = 13;
// here even though a is local its reference gives access to the ''a'' in main()
}
Here I take an integer ''a'' and pass it to 2 functions. One by value and the other a reference. See how a reference can be used to change the original variable. Value only sends the value, literally; a copy of the variable.
#include <iostream.h>
void pass_by_reference(int *a);
void pass_by_value(int a);
main(void)
{
int a;
cout << "Enter ''a'' value\n";
cin >> a; //gets input from keyboard
cout << "Value entered:" << a << "\n"; // prints the integer
pass_by_value(a); //sends a copy of the value of ''a''
cout << "Pass by value:" << a << "\n"; // after the function exe
pass_by_reference(&a);
//sends the address of ''a'', so a''s value can be changed
cout << "Pass by reference:" << a << "\n";
// prints a''s new value changed by the function i.e. 13 here
return 0;
}
void pass_by_value(int a)
{
a = 13;
// a is local to this function so ''a'' in the main() doesn''t change
}
void pass_by_reference(int *a)
{
*a = 13;
// here even though a is local its reference gives access to the ''a'' in main()
}
Actually, your pass by reference function actually passes a pointer
When you pass by value, it passes a copy into the function, a prime example of why you should never pass by value into a function unless you REALLY need to.
Look into copy constructors.
When you pass by value, it passes a copy into the function, a prime example of why you should never pass by value into a function unless you REALLY need to.
Look into copy constructors.
passing an array by copy is different to passing a value by copy.
as it turns out, passing an array by copy actually gets converted to passing an array by pointer - it does indeed NOT copy the array. Watch out for that. Look at and compile the following code to show some different strange nomenclature, and ways of manipulating arrays
[edited by - Shrew on February 3, 2003 8:53:59 AM]
as it turns out, passing an array by copy actually gets converted to passing an array by pointer - it does indeed NOT copy the array. Watch out for that. Look at and compile the following code to show some different strange nomenclature, and ways of manipulating arrays
#include <iostream>using namespace std;void IntCpyFunc( int in) {in = 10;}void IntPtrFunc( int* in) {*in = 20;}void IntRefFunc( int& in) {in = 30;}// cant have size checking - can pass any size (even though specified as in[ 10])void ArrayCpyFunc( int in[ 10]) {in[ 0] = 110;} // following two lines works in EXACTLY the same way as the previous line//void ArrayCpyFunc( int in[]) {in[ 0] = 110;} //void ArrayCpyFunc( int* in) {in[ 0] = 110;} // have to pass in an array of exactly 10 intsvoid ArrayRefFunc( int (&in)[ 10]) {in[ 0] = 130;} // and for fun (by pointer to fixed size array)void FullyFixedDimension( int in[ 10][ 10]){ in[ 5][ 8] = 1000;}// by pointer to 2 dimensional single dimension fixed arrayvoid PartFixedDimFunc( int in[][ 10]){ in[ 0][ 0] = 100; in[ 1][ 0] = 200; in[ 0][ 1] = 300; in += 5; // look what dimension this advances on... in[ 0][ 0] = 400;}void main( void){ int value = 0; cout << value << endl; IntCpyFunc( value); cout << value << endl; IntPtrFunc( &value); cout << value << endl; IntRefFunc( value); cout << value << endl; int valueArray[ 10]; ArrayCpyFunc( valueArray); cout << valueArray[ 0] << endl; ArrayRefFunc( valueArray); cout << valueArray[ 0] << endl; int bigarray[ 10][ 10]; FullyFixedDimension( bigarray); // valid call int notsobigarray[ 7][ 10]; //FullyFixedDimension( notsobigarray); // compile time error as array does not match size PartFixedDimFunc( bigarray); // this works PartFixedDimFunc( notsobigarray); // and so does this! cout << notsobigarray[ 0][ 0] << endl; cout << notsobigarray[ 0][ 1] << endl; cout << notsobigarray[ 1][ 0] << endl; cout << notsobigarray[ 5][ 0] << endl; return 0;}
[edited by - Shrew on February 3, 2003 8:53:59 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement