Z
You have a function parameter. foo declared as type-indicator int *void foo(int* Array);
^^^^^^^^^^
Consequently, within the function,sizeof(Array)/sizeof(int)
equivalentsizeof(int *)/sizeof(int)
If, for example, the size of the indicator, that is, int *8 baytes, and type size int It's 4 bikes, and you'll get 2. If the size of the type int It's also 8 Baitums (64-bit OS), you'll get 1 in the end.But even if you declare this function asvoid foo(int Array[]);
or evenvoid foo(int Array[10]);
Still, the function parameter is not clearly converted into an index to the mass element. That is, the two announcements of the functions declare the same function and equival the next announcement.void foo(int* Array);
So, inside your function, you'll be dealing with the index again.When the mass is transmitted by weight, you should also declare a second parameter that commands the size of the mass. Or the mass should have a certain boundary element with a unique meaning that the number of relevant elements can be determined, as is the case, for example, with the rows when the lines end with zero, i.e. a symbol '\0'♪I mean, in general, you should declare a function asvoid foo(int* Array, size_t n);
where n - the size of the mass.Another approach is to declare a parameter as a reference to the mass. In this case, the length of the mass will be known within the function. For examplevoid foo( int ( &Array )[10] )
{
const size_t = sizeof( Array)/ sizeof( *Array );
}
The weakness of this announcement is that this function can only be dealt with by the masses assigned in its size parameters.In order to circumvent this restriction, you can declare a template function. For example,template <size_t N>
void foo( int ( &Array )[N] )
{
const size_t n = N;
}
In this case, the compiler, using the template, will create as many functions as the various lengths have been used as an argument.