#include <iostream>
void ArraySize(int arrMyarr[])
{
std::cout << sizeof(arrMyarr) << '\n';
}
void ArraySize1(int *arrMyarr)
{
std::cout << sizeof(arrMyarr) << '\n';
}
int main()
{
int arrTemp[] = { 122, 11, 22, 63, 15, 78, 143, 231 };
std::cout << sizeof(arrTemp) << '\n';
ArraySize(arrTemp);
ArraySize1(arrTemp);
return 0;
}
output: 32 4 4
Does below two declarations of functions are same?
void ArraySize(int arrMyarr[]);
void ArraySize1(int *arrMyarr);
Does below two declarations of functions are same?
Yes, completely. Arrays decay to a pointer to their first element when passed like that which is why you're seeing 4. To prevent this, either use std:vector::size() or std::array (C++11) for a compile time sized C-like array.
Does below two declarations of functions are same?
void ArraySize(int arrMyarr[]);
void ArraySize1(int *arrMyarr);
Yes, exactly the same. In the context of function parameter, arrMyarr[] in converted to *arrMyarr implicitly. This is so-called "array decays to pointer".
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