cookies[Arsize]
如果是输出输出地址的长度,比如sizeof cookies,这时输出的是整个数组长度
假如传递给了指针arr, sizeof arr 就输出的是指针的长度
可能理解比较抽象,我们采用实际代码举例:
代码中有详细注释
//通用的统计数组的和的函数
#include<iostream>
const int ArSize = 8;
using namespace std;
int sum_arr(int arr[], int n);
void print();
int main()
{
int cookies[ArSize] = { 1, 2, 4, 8, 16, 32, 64, 128 };
cout << cookies << " \t 数组地址" << endl;
cout << sizeof cookies << " \t sizeof cookies\n";
int sum = sum_arr(cookies, ArSize);
cout << sum << "一共加起来就是这么多" << endl;
print();
//欺骗做法
cout << "这里展示的是传递数组时的特性" << endl;
cout << "前三个的和是:" << sum_arr(cookies, 3)<<endl<<endl;
cout << "结果OK" << endl;
print();
cout << "这里展示的是传递数组时的特性" << endl;
cout << "后四个的和是:" << sum_arr(cookies + 4, 4) << endl << endl;
cout << "结果OK" << endl;
cin.get();
return 0;
}
int sum_arr(int arr[],int n)
{
int total = 0;
cout << arr << "\t arr地址" << endl;
cout << sizeof arr << "\t sizeof arr\n";
for (int i = 0; i < n; i++)
total = total + arr[i];
return total;
}
void print()
{
cout << endl;
cout << "=======================================" << endl;
cout << endl;
}
输出结果: