Hello,
how to get the item count of an array of structs?
For example:
sizeof(ar) is wrong!?Code:struct intArr
{
int i;
int y;
}
intArr ar[10]; // here!
thanks
break;
Printable View
Hello,
how to get the item count of an array of structs?
For example:
sizeof(ar) is wrong!?Code:struct intArr
{
int i;
int y;
}
intArr ar[10]; // here!
thanks
break;
Quote:
Originally Posted by break;
This only works if the code is placed where "ar" is really an array, not a pointer.Code:sizeof(ar) / sizeof(ar[0]);
It will not work if you pass "ar"' to a function, and you attempt to get the number of elements from the passed in parameter. Passing an array to a function just passes a pointer, not the entire array.
Regards,
Paul McKenzie
Hello,
thanks for your help!
regards
break;
[ Moved thread ]
I like this method better:
Code:sizeof(ar) / sizeof(intArr)
Or you could use the Standard Template Library
Code:#include <vector>
struct intArr
{
int i;
int y;
}
std::vector<intArr> ar(10);
std::vector<intArr>::size_type size = ar.size();
Actually, I think that form is more confusing. Dividing the size of the entire array by the size of the first element is more intuitive.Quote:
Originally Posted by spoulson
My $0.02..
Viggy