English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want to pass arrays in the function I write in C++. How to do that? When I pass single variable, its easy. But when arrays are taken as input the size have to be defined in the first place. Please help.

2007-11-02 22:21:04 · 4 answers · asked by arnab 2 in Computers & Internet Programming & Design

4 answers

There are a few ways to do this, not one. Let us look at them:

1. The straightforward way: Say you have an int [] array that you want to pass to a function func(). You can define func() like this:

void func( int [] iArrayParam, int iArrayLength )
{...}

Note: The above definition is same as
void func( int * iArrayParam, int iArrayLength )
{...}

Then, from the calling function, you can call func() with the array as the 1st argument, and its length as the 2nd. Like:

...
int iArray[ 5 ] = { 34, 35, 0, 2, 41 };
...
func( iArray, 5 };
....

2. The above method is crude and naive. Some people like to be more sophisticated and instead of passing the length, they populate the array with a delimiter value. Because inside func(), you will probably traverse the array and that is WHY you need to know the length. But you can also traverse an array if you, instead of the length, knew the last value.

This is how it works: Say you need to pass an int array to func(). You need to pass the 5 values 34, 35, 0, 2, 41 in it. So, from the calling function, you crate an array of length 6 (not 5). In the last place, you put a value like -1. Be careful while choosing this value - it must be a value which the other elements CANNOT BE. I mean you can choose -1 ONLY IF implementation logic prevents any other element of the array to be -1. So, your func() looks like this:

void func( int * iArrayParam )
{
...
int iCnt = 0;
while( iArrayParam[ iCnt ] != -1 )
{
//Do something with iArrayParam[ iCnt ]...
//Note that you aren't using array length to traverse.
}
...
}

And you call it like this:
...
int * iArray = new int [6];
iArray[0] = 34;iArray[1] = 35;iArray[2] = 0;
iArray[3] = 2;iArray[4] = 41;
iArray[5] = -1; //This is the delimiter value
func( iArray );
...

3. The above method (#2) is what C++ uses in strings - A C style string always ends in NULL ('\0'). So basically when you pass a string (which is nothing buta char array) to a function, you do not state its length - because there is an unstated understanding between the functions that the array will end in a string null - ASCII zero.

Lastly, it is your imagination. Func() needs some way to know where the passed array ends. You, as developer, can devise more and more clever ways to tell that to func(). You can create a class that encapsulates the array and also holds the array length as a public property. But before you get all excited and start writing such a class, hold on! That has already been done! Why not use STL vector instead of a simple array?

So instead of using int[], all you do is #include and may be write

using namespace std;

on top, and declare and populate:

vector vMyIntegerArray;
vMyIntegerArray.push_back( 34 );
vMyIntegerArray.push_back( 35 );
...

Finally, pas it to func, which looks like:

void func( vector v )
{
//Use v.length() to access the length of the array, oops, vector
}

2007-11-03 04:43:15 · answer #1 · answered by Koushik Biswas 3 · 0 0

The easiest solution is to pass a pointer to the array, and to specify the size of the array in other variables.

If you want, you can create a structure that holds both the size of the array and the array contents, and pass a pointer to the structure. That's basically how Turbo Pascal handles strings - the first byte of the structure specifies the length of the string, and the rest of the structure holds the string itself.

When you pass variables in C (and consequently in C++), you are copying those variables to the stack. When you have an array, that's not only slow and inefficient, you may also run out of stack.

In C, there is no difference between arrayname[5] and 5[arrayname]. The compiler simply adds together arrayname and 5 together, using pointer arithmetic, and then dereferences the pointer. Multi-dimensional arrays are actually single-dimensional arrays; that's why you have to declare the size of a multi-dimentional array. Without the declaration, there's no way for the compiler to calculate whether arrayname[5][3] is actually arrayname[83] or arrayname [123].

You might want to ignore Mantrid's answer; he doesn't seem to understand that there's a difference between an array and an ASCIIZ string. You have to pass the size when you are passing an array.

2007-11-03 05:35:25 · answer #2 · answered by Anonymous · 0 0

Pass pointers, array == pointer in C/C++

2007-11-03 05:30:22 · answer #3 · answered by Andy T 7 · 0 1

If you're talking about C-style arrays and not STL ones, you can pass a pointer to the first element of the array. example-

void toUpperCase( char* ptr)
{
int i =0;
while (ptr[i] != 0)
{
ptr[i] = toupper(ptr[i]);
++i;
}
}

2007-11-03 05:29:14 · answer #4 · answered by Mantrid 5 · 0 1

fedest.com, questions and answers