Interview Questions

I want to know how many elements are in an array, but sizeof yields the size in bytes.

C Interview Questions and Answers


(Continued from previous question...)

I want to know how many elements are in an array, but sizeof yields the size in bytes.

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]);

(Continued on next question...)

Other Interview Questions