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]);
本文介绍了一种简单的方法来计算C/C++中数组的元素数量。通过将整个数组的大小除以单个元素的大小,可以得到数组中元素的确切数量。
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]);

被折叠的 条评论
为什么被折叠?