how use arrays on funtion parameter?

Mar 27, 2018 at 7:26pm
these function give me the array size:
1
2
3
4
void GetArrayElementsCount(int arraycount[])
{
    cout << sizeof(arraycount)/sizeof(arraycount[0]) << "\n";
}

but, because i use an array parameter, i'm getting wrong results.
and 1 warning:
warning: 'sizeof' on array function parameter 'arraycount' will return size of 'int*' [-Wsizeof-array-argument]
why these warning?
Last edited on Mar 27, 2018 at 7:27pm
Mar 27, 2018 at 7:40pm
sizeof(x) / sizeof(x[0]) only tells you the length of the array x if it's known in the current scope. GetArrayElementsCount() is not taking an array as a parameter, it's taking a pointer. These two declarations are equivalent:
1
2
void foo(int x[]);
void foo(int *x);
There's no way for GetArrayElementsCount() to know the size of the array you pass when you define it like this.

You can define it like this, however:
1
2
3
template <size_t N>
void GetArrayElementsCount(int (&arraycount)[N]){
    //... 
But at that point you no longer need to use the sizeof trick:
 
    std::cout << N << "\n";

This is a common mistake newbies make. In C and C++, if you want to pass arrays around you have to pass the size also. There's otherwise no way for the callee to know the size of the array you're passing.
Mar 27, 2018 at 7:41pm
When passing an array like that, the size of the array is lost. arraycount is effectively just an int*. Just a pointer.

The C++ standard allows this; the C standard makes it compulsory.
Mar 27, 2018 at 7:53pm
helios can you explain more about that parameter type?
i never did like that. event that template type(only the 'typename' and 'class')
Mar 27, 2018 at 8:01pm
If you call GetArrayElementsCount() for example like this:
1
2
int array[42];
GetArrayElementsCount(array);
the compiler will fill in 42 into the value of N automatically. You could also pass it yourself explicitly, but it's unnecessary:
1
2
GetArrayElementsCount<42>(array); //OK
GetArrayElementsCount<53>(array); //Error 

Or you could define GetArrayElementsCount for a specific size:
1
2
void GetArrayElementsCount2(int (&arraycount)[42]){
    //... 
But then you can only pass arrays of that size:
1
2
3
GetArrayElementsCount2(array); //OK
int array2[71];
GetArrayElementsCount2(array2); //Error 
Mar 27, 2018 at 8:07pm
- so the 'size_t' it's for arrays size. we don't need pass it, because the template do the work;
- '&' means for use the original variable and not the copy;
but why the parentheses?
Mar 27, 2018 at 8:11pm
reference to an array
vs
array of references
Mar 27, 2018 at 8:24pm
i'm review the C++ book. maybe i can find more information.
thank you so much for all to all.
thank you
Mar 27, 2018 at 8:27pm
There's an opportunity to be more generic here: const T& could be used to make it work for both C and C++ arrays (and C++ vectors etc):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <array>
#include <vector>
#include <iterator>
#include <iostream>

template <class T>
void GetArrayElementsCount(const T& a){
    std::cout << std::size(a) << '\n'; // requires C++17
}

int main() {
    int a[10];
    GetArrayElementsCount(a);
    std::array<int, 10> b;
    GetArrayElementsCount(b);
    std::vector<int> c(10);
    GetArrayElementsCount(c);
}

live demo: https://wandbox.org/permlink/3fOwxyq58baWQMLZ
Last edited on Mar 27, 2018 at 8:27pm
Mar 27, 2018 at 8:30pm
for now, i use C++11.. maybe i can use C++ 14. i don't have sure if C++14 is from 2014 year
Mar 27, 2018 at 8:54pm
- '&' means for use the original variable and not the copy;
No, you can't pass a C array by copy[1]. If you just declare
1
2
template <size_t N>
void GetArrayElementsCount(int arraycount[N]);
this is the same as
 
void GetArrayElementsCount(int *arraycount);


[1] Unless it's part of an object and you pass the entire object, as in the case with std::array.
Mar 27, 2018 at 9:09pm
1
2
3
4
5
template <typename type, size_t N>
int GetArrayElementsCount(type (&arraycount)[N])
{
    return (sizeof(arraycount)/sizeof(arraycount[0]));
}

'(&arraycount)' - it's the adressed variable;
'[N]' - only accepts an array. the 'N' it's added automatic.
PS: these way is learned on Template chapter?
Last edited on Mar 27, 2018 at 9:11pm
Topic archived. No new replies allowed.