passing an array by pointer or refrence?

Started by
13 comments, last by Nothingness 21 years, 2 months ago
Ooops sorry! My Mistake, bad typo!
Advertisement
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()
}
The sky is the limit !
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.
*st0ned*
void functionref(int (&value)[10]);
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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


  #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]
My company - Aah Games - http://www.aahgames.com

This topic is closed to new replies.

Advertisement