+ 2
How can I get array size in function call? 010000110010101100101011
When I pass a array in function, taking the size of the array seems to give the size of the array pointer: https://code.sololearn.com/cbdfREro7Y7h/#cpp I know I could just pass take the size from outside the function call and pass it as an argument, but I would want to know if there is a way to get the size of the array inside the function call, using the sizeof operator: void printArrayLength(bool arr[]) { cout << sizeof ??? << endl; }
5 Réponses
+ 3
Interesting question.
I found some stuff on SO:
https://stackoverflow.com/questions/968001/determine-size-of-array-if-passed-to-function
It seems it is not possible to determine the size of array from inside a function, if you pass it as a pointer.
Some workarounds they suggested :
- use a vector instead
- if the array size is constant, store the size in a constant too
- if the size is known at compile time, you can define a template function and pass the array as reference
- pass the size along in a separate argument
- use a sentinel value to mark the end (not too practical with bool...)
+ 2
Tibor Santa ~ swim ~ Return thanks for your arguments; weird the arg array size can't be calculated inside the function call, but let's just calculate the size outside the function call.