Is there any way to print length of an array which contains an element 0 and that array has been passed to a function which can have only one argument as the array?
Example:
int arrLen(int arr[])
{
//get size here;
}
int main()
{
int arr[]={0,1,2,3,4,5};
arrLen(arr);
}
There is a limitation in C++ that we can not compare the elements of an array with a NULL if it has a zero, but still asking if there is a way to do that. I can only pass array to function is my limitation.
In your very example, you can use function template to get what you want:
template <size_t N>
int arrLen(int (&arr)[N])
{
return N;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With