代碼
#include <iostream>
using namespace std;int main()
{//指針和數組//利用指針訪問數組中的元素int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };cout << "第一個元素為:" << arr[0] << endl;int * p = arr;//arr就是數組首地址cout << "利用指針訪問第一個元素:" << *p << endl;p++;//讓制作向后偏移4個字節cout << "利用指針訪問第二個元素:" << *p << endl;cout << "利用指針遍歷數組:" << *p << endl;int * p2 = arr;for (int i = 0; i < 10; i++){cout << *p2 << " ";p2 ++;}cout << endl;system("pause");return 0;
}