Switch to full style
C++ code examples
Post a reply

Array Passing

Thu Nov 13, 2008 2:54 pm

Passing arrays in C++
cpp code
/*
* Demonstrate some peculiarities of array passing in C.
*
* Author: Tom Bennet
*/
#include <iostream>

using namespace std;

// Each version receives the same size-5 integer array from the main pgm.

// Receive it as declared in main.
void rcvA(int arr[5])
{
cout << "A [" << sizeof arr << "]" << endl;
for(int m = 0; m < 5; m++)
cout << arr[m] << " ";
cout << endl;
}

// Receive it as the system sends it -- a pointer to the first item in the
// array.
void rcvB(int *arr)
{
cout << "B [" << sizeof arr << "]" << endl;
for(int m = 0; m < 5; m++)
cout << arr[m] << " ";
cout << endl;
}

// Since it sends a pointer and ignores the array size anyway, the system
// lets you leave it out here, too.
void rcvC(int arr[])
{
cout << "C [" << sizeof arr << "]" << endl;
for(int m = 0; m < 5; m++)
cout << arr[m] << " ";
cout << endl;
}

// In fact, so long as the system is going to ignore the size anyway, we can
// write what we want. The 1024 doesn't make the array that large,
// however; it's however large its creator made it. rcvD can't find out.
void rcvD(int arr[1024])
{
cout << "D [" << sizeof arr << "]" << endl;
for(int m = 0; m < 5; m++)
cout << arr[m] << " ";
cout << endl;
}

main()
{
// Here's the array all the fuss is about.
int arr[5];

// Put some stuff there.
for(int i = 0; i < 5; ++i)
arr[i] = 2*i -3;

// Do exactly what the functions do.
cout << "M [" << sizeof arr << "]" << endl;
for(int m = 0; m < 5; m++)
cout << arr[m] << " ";
cout << endl;

// Now run the functions.
rcvA(arr);
rcvB(arr);
rcvC(arr);
rcvD(arr);
}


passing arrays to functions in c++ :grin:



Post a reply
  Related Posts  to : Array Passing
 Passing a Reference Variable     -  
 Passing Pointers to function example     -  
 Passing an Argument to a Function by Value     -  
 JSP Passing Arrays to Methods     -  
 passing string value from java to .exe file     -  
 Passing Enum as Type Parameter to method     -  
 Passing arrays as function parameter in java     -  
 Array difference for associate array     -  
 compare an array with another array?     -  
 Pop the element off the end of array     -  

Topic Tags

C++ Arrays