How To Pass An Array To A Function

Table of contents:

How To Pass An Array To A Function
How To Pass An Array To A Function

Video: How To Pass An Array To A Function

Video: How To Pass An Array To A Function
Video: Passing Array as an Argument to a Function 2024, December
Anonim

Arrays are one of the most heavily used forms of structured data storage in computer programs. Their processing can be done by various algorithms implemented in class methods and functions. Accordingly, it is often required to pass an array to a function. The C and C ++ languages offer great freedom in choosing the methods of performing this action.

How to pass an array to a function
How to pass an array to a function

It is necessary

compilers of C and C ++ languages

Instructions

Step 1

Pass a fixed-size array to the function. Change the prototype of the function to contain an argument of the appropriate type. For example, the declaration of a function that takes an array of integer numeric values of three elements as a parameter might look like this:

void ArrayFunction (int aNumbers [3]);

Such a function is called by passing an array directly to it as an argument:

void SomeFunction ()

{

int aNumbers = {1, 2, 3};

ArrayFunction (aNumbers);

}

The transferred data is copied onto the stack. Modifying the array in the called function does not change the source.

Step 2

Pass variable length arrays to the function. To do this, simply do not specify the dimension of the corresponding argument:

void ArrayFunction (int aNumbers );

Multidimensional arrays can also be passed in a similar way (only the first "dimension" can be variables):

void ArrayFunction (int aNumbers [3] [2]);

These functions are called in the same way as in the first step.

In order to be able to correctly process arrays of variable length in a function, you must either explicitly pass the number of their elements through an additional parameter, or use conventions that impose restrictions on the values of the elements themselves (a certain value must be a sign of the end of the array).

Step 3

Pass the array by pointer. The function argument must be a pointer to a value with a type corresponding to the elements of the array. For example:

void ArrayFunction (int * pNumbers);

Access to data in a function can be carried out both in the notation for working with array elements, and using address arithmetic:

void ArrayFunction (int * pNumbers)

{

pNumbers [0] = 10; // access to element 0

* (pNumbers + 1) = 20; // access to item 1

}

Be careful! Since the function is passed not a copy of the data, but a pointer to it, the original array will be modified.

The advantage of this method is speed, economy of computational resources and a certain flexibility. So, you can call the target function by passing it a pointer to an arbitrary element of the array:

void SomeFunction ()

{

int aNumbers = {1, 2, 3};

ArrayFunction (aNumbers); // whole array

ArrayFunction (& aNumbers [1]); // starting from the second element

}

This method also usually involves passing the number of available elements in an additional parameter or using an array terminator.

Step 4

Pass data to a function with a parameter that is an object or a reference to an object of the class that implements the array functionality. Such classes or class templates are usually found in popular libraries and frameworks (QVector in Qt, CArray in MFC, std:: vector in STL, etc.).

Often these classes implement an implicit data sharing strategy with reference counting, performing a deep copy only when the data is modified (copy on write). This allows you to minimize the consumption of computational resources even in the case of passing array objects by value through the arguments of functions and methods:

void ArrayFunction (QVector oArray)

{

int nItemCount = oArray.count ();

int nItem = oArray [0];

}

void SomeFunction ()

{

QVector oArray (10);

for (int i = 0; i

Recommended: