this prints 20....where does 20 come fromCode:int len [5]; cout<<sizeof(len);
this prints 20....where does 20 come fromCode:int len [5]; cout<<sizeof(len);
An int is 4 bytes, and you've declared an array of 5 ints. So 4*5=20 bytes.
M.Eng Computer Engineering CandidateB.Sc Computer Science
Robotics and graphics enthusiast.
so how do i get the length of the array besides sizeof() * 4;
also,
int arr [ size ];
when i do memset, i have to do
memset( arr, 0 , size*4 ) ?
memset( arr, 0 , sizeof(arr) )
Last edited by l2u; 12-24-2006 at 08:39 PM.
and getting length of array (not number of bytes)? (besides sizeof / 4)
sizeof(arr) / sizeof(arr[0])
Although that doesn't work if you have passed the array to a function or have just a pointer to it. In those cases, you need to keep track of the size yourself.
In C++, it is generally better to use vector (or std::tr1::array) which remember their own sizes.
> memset( arr, 0 , size*4 ) ?
So what's wrong with doing
for ( int i = 0 ; i < size ; i++ ) arr[ i ] = 0;
memset is a very low level function which has no knowledge of types. The for loop approach is both intuitively obvious, and will continue to work on arrays of all types, not just ints.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.