How To Return An Array

Table of contents:

How To Return An Array
How To Return An Array

Video: How To Return An Array

Video: How To Return An Array
Video: How to return an array from a function 2024, May
Anonim

Arrays are one of the simplest forms of structured data storage. Since indivisible memory areas are allocated for storing one-dimensional arrays, and most programming languages have syntactic constructs for their declaration, they are intensively used as buffers for transferring large amounts of information to various (including library) functions. In turn, functions sometimes need to return an array.

How to return an array
How to return an array

Necessary

  • - text editor;
  • - C or C ++ compiler and linker.

Instructions

Step 1

Return data from a function or method by placing it in a fixed-length array, allocated by the caller, and passed by reference. Define the appropriate data type, for example: typedef int t_array10 [10]; Use it in the declaration and implementation of the required function or method: void ReturnArray (t_array10 & raOut) {raOut [0] = 10;} Call accordingly: int aNumbers [10]; ReturnArray (aNumbers); The disadvantage of this method is that the length of the array is fixed.

Step 2

Return the array data by placing it in a caller-allocated output buffer passed by pointer. The prototype of a function or method can contain a parameter declared both in array notation without size specification: void ReturnArray (int anArray , int nSize); or as a pointer: void ReturnArray (int * pnArray, int nSize); In both cases, the identifier parameter can be used in array notation: void ReturnArray (int * pnArray, int nSize) {for (nSize--; nSize> -1; pnArray [nSize] = nSize--);} // call int aNumbers [10]; ReturnArray (aNumbers, sizeof (aNumbers) / sizeof (aNumbers [0])); This method of passing arrays to functions and returning array data from them is used in the overwhelming majority of cases when developing C programs. Note that functions and methods that accept and returning data in this way, as a rule, must have an additional parameter through which the actual size of the buffer is passed.

Step 3

Return an array created in a function or method. Formally, in this case, not an array should be returned, but a pointer to its first element: int * ReturnArray () {int * pnArray = new int [10]; // work with pnArray return pnArray;} // get a pointer to the created array int * pnNumbers = ReturnArray (); // use delete pnNumbers; // delete This method has some limitations. So, the array must be located in heap. In addition, its deletion must be performed by the method corresponding to the creation (delete in case of using new, free for malloc, etc.).

Step 4

Use container classes to conveniently return arrays when developing C ++ programs. An example of creating and returning an array that is an object of the vector template class of the C ++ Standard Template Library specified for int might look like this: #include std:: vector ReturnArray () {std:: vector oArray; oArray.resize (10); oArray [0] = 100; oArray [1] = 200; return oArray;} std:: vector oArray = ReturnArray (); Note that most of the popular frameworks provide powerful classes or array class templates. They often implement the concept of implicit data sharing with reference counting and copy-on-write. Therefore, the return by value of such arrays from functions is performed very efficiently and is associated with a minimum consumption of computing resources.

Recommended: