If you want to give people a chance to understand what it is doing, you might also try
Code:
#include <iostream>
int main()
{
int x[] = {1,2,3,4,5};
std::cout << x[sizeof(x)/sizeof(*x) - 1] << std::endl;
}
And if you want to make it even cleaner, and at the same time let the compiler ensure that you are in fact dealing with an array (and not an array that has decayed to a pointer and thus lost track of the array's size):
Code:
#include <iostream>
template <class T, unsigned N>
unsigned array_size(const T (&)[N])
{
return N;
}
int main()
{
int x[] = {1,2,3,4,5};
std::cout << x[array_size(x) - 1] << std::endl;
}