Q: I want to know how many elements are in an array, but sizeof yields the size in bytes.
A: Simply divide the size of the entire array by the size of one element:
int array[] = {1, 2, 3};
int narray = sizeof(array) / sizeof(array[0]);
Q: I want to know how many elements are in an array, but sizeof yields the size in bytes.
A: Simply divide the size of the entire array by the size of one element:
int array[] = {1, 2, 3};
int narray = sizeof(array) / sizeof(array[0]);