Mariojulio Z. answered 04/03/19
Experienced university tutor in computer science and game dev.
Hi,
It is actually very easy. You just need to get the size in bytes of the entire array and divide that number by the size in bytes of one of the array's element. For instance:
int a[10];
size_t num_elements = sizeof(a) / sizeof(a[0]);
Explanation: (assuming int size is 4 bytes)
sizeof(a) = size_of_1_element * number_of_elements = 4 bytes * 10 bytes = 40 bytes.
sizeof(a[0]) = 4 bytes.
Then, num_elements = 40 bytes / 4 bytes = 10 bytes.