The output of the following function is "int *", which means the formal parameter is converted to a integer pointer. Is there any necessary reason for this design? Why can't we reserve the array type?
// the output is "int *"
#include<typeinfo>
void Func(int ar[5])
{
printf("%s\n", typeid(ar).name();
}
int main()
{
int ar[5];
Func(ar);
return 0;
}
Is there any necessary reason for this design?
This is historical baggage from C. Supposedly 1 this was convenience as you can't pass arrays by-value anyway.
If you want to preserve the type, you can use a references or pointers:
void Func(int (&ar)[5]);
Or using template functions to accept an arbitrarily sized array:
template<std::size_t N> void Func(int (&ar)[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