Hello,for those who knew how to return number of elements array usually use 'sizeof( arrayNumber ) / sizeof( arrayNumber[ 0 ] )'. However, I don't quite understand the code behind it. Why need to divide into first element of the array ?
1 2 3 4 5 6
int main()
{
int arrayNumber[] = {1,2,3,4,5,6,7,8,9};
int numberElementArray = sizeof( arrayNumber ) / sizeof( arrayNumber[ 0 ]);
cout << "Number of element in arrayNumber is: " << numberElementArray;
}
sizeof(arrayNumber) is the total number of bytes occupied by the array in memory, which is not 9 in this case. sizeof(arrayNumber[0]) is the size of one element in the array, which is not 1 in this case.
Try printing out each size separately and do some mental math.
By the way, this is a bad way to detect the number of elements in an array - prefer instead to use something specifically designed to do that: http://en.cppreference.com/w/cpp/types/extent
Or in C++03: