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?
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.
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.
- 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?
'(&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?