指针修改数组变量

指针的基本知识

C++版

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{

    char ch[6] = "hello";
    char* pch = ch;
    // char数组可以用指针来修改
    pch[0] = 'x';
    char *p = "hello";
    // "hello"本身已经是一个字符串常量,不是数组所以不能修改
    p[0] = 'H';
    int a[3] = {1,2,3};
    int *b = a;
    // int数组可以用指针来修改
    b[0] = 4;
    for(int i = 0; i < 3; i++){
        cout<<a[i]<<endl;
    }
    cout<<pch<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/flyingrun/p/13379528.html
今日推荐