指针基础实践

1.声明指针

2.初始化指针

3.指针地址和大小,值

4.指针解引用,修改值

5.指针指向堆内存,修改值

6.申请堆内存并释放

7.数组释放

8.指针运算

9.指针递增

10.指针递减

11.指针常量

12.常量指针

13.常量指针指向常量

#include <iostream>
#include <string>

using namespace std;

/*
* 1.声明指针
* 2.初始化指针
* 3.指针地址和大小,值
* 4.指针解引用,修改值
* 5.指针指向堆内存,修改值
* 6.申请堆内存并释放
* 7.数组释放
* 8.指针运算
* 9.指针递增
* 10.指针递减
* 11.指针常量
 *12.常量指针
 * 13.常量指针指向常量
* */

int main(){
    int * i;
    string * s;

    int age = 10;
    int * p = &age;
    int * dogName = nullptr;

    cout << "指针的地址:"<< &p << endl;
    cout << "指针的值:"<< p << endl;
    cout << "age得地址:"<< &age << endl;
    cout << "指针的大小:"<< sizeof(p) << endl;
    cout << "age的值:"<< age << endl;

    string name = "小明";
    string *n = &name;
    cout << "指针解引用:"<< *n << endl;
    *n = "小红";
    cout << name << *n << endl;

    int * score = new int(10);
    *score = 100;
    cout << "score="<<*score << endl;

    int * p2 = new int;
    p2 = nullptr;
    delete p2;

    int *arr = {nullptr};
    arr = new int[3];
    delete[] arr;

    int id[] = {1,2,3};
    int *pd = id;
    cout << "pd得值" << pd << endl;
    cout << "id得地址:"<< id << endl;

    int score2[] = {10,20,30};
    int *pscore = score2;
    cout << *pscore << "==" << score2[0] << endl;
    cout << *(pscore+1) << "==" << score2[1] << endl;

    for (int j = 0; j < 3; ++j) {
        cout << *pscore <<endl;
        pscore++;
    }

    int score3 = 59;
    const int * p3 = &score3;
    // 只能修改指针的指向,不能修改值
//    p3 = 100;
    int age3 = 18;
    p3 = &age3;
    cout << *p3 << endl;

    int * const p4 = &score3;
//    p4 = &age3;
    *p4 = 69;
    cout << *p4 << endl;

    const int * const p5 = &score3;
//    p5 = &age3;
//    *p5 = 102;
    cout << p5 << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值