C++中指针和数组

175 阅读1分钟
#include<iostream>
using namespace std;

/*
指针和数组
*/

int main(){
    
    //利用指针访问数组中的元素
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};

    int * p = arr;  //arr就是数组首地址
    cout << "利用指针访问第一个元素:" << *p << endl;
    p++; //让指针向后偏移4个字节
    cout << "利用指针访问第二个元素:" << *p << endl;
    
    int * p2 = arr; //arr就是数组首地址
    for (int i = 0; i< 10; i++)
    {
        cout << *p2 << endl;
        p2++;
    }
    system("pause");
    return 0;
}