C++ 覺得奇妙的傳陣列方法
C++ 覺得奇妙的傳陣列方法
延伸閱讀:C++ Primer(4/e) P238 7.2.4. Array 參數
#include <iostream>
using namespace std;
const int ARRAY_NUM = 5;
void Array(int p[ARRAY_NUM]);
int main()
{
int p1[ARRAY_NUM] = { 1, 2, 3, 4, 5 };
cout <<"sizeof(p1) = " << sizeof(p1) << endl; //20
Array(p1);
system("pause");
return 0;
}
void Array(int p[ARRAY_NUM]) //這個陣列裡的數字其實寫什麼都沒差
{
cout << "sizeof(p) = " << sizeof(p) << endl; //4(是指標)
cout << endl;
for (int i = 0; i < 5; i++)
cout << p[i] << endl;
cout << endl;
for (int i = 0; i < 5; i++)
cout << *(p++) << endl;
}
留言
張貼留言