Array size determination within a function | SoloLearn: Learn to code for FREE!

+2

# Array size determination within a function

In the 'main' part of the programm I declared an array of N elements. I would like to send it to the function which make some statistical calculation on it. How is it possible to detemine the size of this array (N) within the function without sending the number explicitely?

1/24/2018 7:24:29 AM

+4

You can use templates, the compiler will automatically deduce the size for you. #include <iostream> template<class T, std::size_t sz> void PrintArray(T (&arr)[sz]) { for (auto& i : arr) std::cout << i << "\n"; } int main() { int arr[] = {4, 3, 1, 5, 8}; PrintArray(arr); } https://code.sololearn.com/c1wqPSpEJnuT

+11

As I said earlier, the method does not work for pointers to arrays. For pointers you must explicitly pass the size. You might want to use an array from the STL: http://en.cppreference.com/w/cpp/container/array or a vector would work.

+9

Generic method of calculating the size of an array, arr: sizeof(arr)/sizeof(arr[0])

+9

@jay Correct. Pointers no, just actual arrays.

+7

Use an inbuilt container? (vector, array) http://www.cplusplus.com/reference/array/array/ https://www.sololearn.com/learn/261/?ref=app

+7

Note: I believe the generic method will not work with pointers. (just in case this is what you were planning)

+3

Thanks a lot! It works well: https://code.sololearn.com/c6nxF46PmV76/#cpp #include <iostream> using namespace std; template<class T, std::size_t sz> void PrintArray(T (&arr)[sz]) { std::cout << "Size of an element: " << sizeof(arr[0]) << "\n"; std::cout << "Size of the whole array: " << sizeof(arr) << "\n"; std::cout << "Number of elements: " << sizeof(arr)/sizeof(arr[0]) << "\n"; std::cout << "Elements of the array: "; for (auto& i : arr) std::cout << i << " "; std::cout << "\n " << "\n"; } int main() { int arr[] = {4, 3, 1, 5, 8}; PrintArray(arr); float farr[] = {1.2, 3.5, 8.1, 9.0, 3.9, 6.1}; PrintArray(farr); double darr[] = {1.22, 3.51, 8.17, 9.05, 9.00, 3.94, 6.18, 5.283}; PrintArray(darr); char carr[] = {'a', 'd', 'f', 's'}; PrintArray(carr); char sarr[] = "just an example"; // string as an array PrintArray(sarr); std::cout << sarr << "\n"; } Output ========================================== Size of an element: 4 Size of the whole array: 20 Number of elements: 5 Elements of the array: 4 3 1 5 8 Size of an element: 4 Size of the whole array: 24 Number of elements: 6 Elements of the array: 1.2 3.5 8.1 9 3.9 6.1 Size of an element: 8 Size of the whole array: 64 Number of elements: 8 Elements of the array: 1.22 3.51 8.17 9.05 9 3.94 6.18 5.283 Size of an element: 1 Size of the whole array: 4 Number of elements: 4 Elements of the array: a d f s Size of an element: 1 Size of the whole array: 16 Number of elements: 16 Elements of the array: j u s t a n e x a m p l e just an example =====================================================

+2

Unfortunately such a construction sizeof(arr)/sizeof(arr[0]) doesn't work within a function. In the example below the sizeof(arr) operator returns just a size of the pointer itself and the value of aSize is always 1. So it is necessary to pass the size of the array explicitely, The question is how to avoid this? float rootMeanSquer(int *arr, int arrSize, float aver) { int i = 0; float rootMeanSquer = 0; int aSize; aSize = sizeof(arr)/sizeof(arr[0]); // A try to determine the number of elements cout << aSize << endl; for( int i = 0; i < arrSize; i++) { rootMeanSquer += sq(arr[i] - aver); }; rootMeanSquer /= (arrSize - 1); return sqrt(rootMeanSquer); } // End rootMeanSquer