指针修改数组变量

指针的基本知识

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;
}
posted @ 2020-07-26 10:35  程序员曾奈斯  阅读(541)  评论(0编辑  收藏  举报