Here's my thinking: sizeof()
is an operator that calculates how big a variable is. sizeof(variable type)
can calculate how big a certain type is. The number of the elements in an array is given by sizeof(<array name>) / sizeof(variable type)
.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
double b[] = {1 , 2 , 3 , 4 , 5};
printf("How many elements the array contains : %d\n" , sizeof(b) / sizeof(double));
system("pause");
return 0;
}
The output is 5 which is correct.
I wonder if there is some more efficient way to calculate that? Say, a C function.