#include "stdafx.h"
struct OBJECT
{
float X, Y, Z;
};
int blabla(OBJECT test[]);
OBJECT test[230];
int _tmain(int argc, _TCHAR* argv[])
{
int g = blabla(test);
return 0;
}
int blabla(OBJECT test[])
{
int one = sizeof(test);
int two = sizeof(test[0]);
int leng = one / two;
return leng;
}
but when I do it like this it works just fine
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include "stdafx.h"
struct OBJECT
{
float X, Y, Z;
};
OBJECT test[230];
int _tmain(int argc, _TCHAR* argv[])
{
int one = sizeof(test);
int two = sizeof(test[0]);
int leng = one / two;
return 0;
}
In example 2, at line 12 the compiler knows that test is of type OBJECT[230] -- that is, it knows how many elements
there are in the array.
In example 1, line 21 is trying to take the size of its parameter. Its parameter is OBJECT[] -- that is, the size is unknown.
(In fact, the size it returns will be sizeof( OBJECT* ), which will be 4 for a 32-bit platform).
You have to make the size known or else pass in the number of elements to the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// But now blabla can only take an array of exactly 230 elements.
int blabla( OBJECT test[ 230 ] ) {
// ...
}
// or
int blabla( OBJECT test[], int numElements ) {
}
// or
template< size_t N >
int blabla( OBJECT test[ N ] )
{
// This function can only be called with fixed length arrays of any size.
}