I need help in calculating the total number of elements stored in an array. For example if I have something like,
int a[10] = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8}; As you can see that the elements of the array are less than the size of the array i.e. 10. Then how do I calculate the total number of elements in the array?
a[10]has 10 elements, the fact that you're not assigning all of them doesn't change this fact. Your compiler most likely initializes the rest to 0std::cout << "Array Length= " << (sizeof(a)/sizeof(*a))<<"\n";orint arrayLength=0; for (auto i : a) {++arrayLength;} std::cout<< "Array Length= " << arrayLength;