在不使用 sizeof 的情况下在 C/C++ 中查找数组的大小
在这个程序中,我们在不使用sizeof的情况下,找出了在C/C++中查找数组的大小。
算法
Begin Initialize the elements of the array. &a => This is the pointer to array which points at the same memory address as a. &a + 1 => It points at the address after the end of the array. *(a+1) => Dereferencing to *(&a + 1) gives the address after the end of the last element. *(a+1)-a => Subtract the pointer to the first element to get the length of the array. Print the size. End.
示例代码
#include输出结果using namespace std; int main() { int a[] = {6,7,5,3,1,4,2,10,9}; int s = *(&a + 1) - a; cout << "数组中的元素数是 "<< s; }
数组中的元素数是 9
热门推荐
4 祝福语简短古诗词
10 圣诞祝福语简短小学
11 孩子16岁祝福语简短
12 同事离别祝福语简短的
13 高考祝福语简短10字
14 许愿星姐妹祝福语简短
15 天气转凉祝福语简短
16 长辈钻石婚祝福语简短
17 导师退休花束祝福语简短
18 简短大气的狗年祝福语