關鍵點:拿到數組首地址。
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int *p = arr;//數組名就是數組首地址
cout << "第一個元素:" << arr[0] << endl;//1
cout << "指針訪問第一個元素:" << *p << endl;//1
/*
利用指針遍歷數組
*/
for (int i = 0; i < 10; i++)
{
cout << *p << endl;
p++;
}
system("pause");
return 0;
}
結果展示: