c++中的指针

c/c++中容纳地址的变量称为指针类型的变量,其运算规则与指针指向的数据类型相关。
赋初值方式有两种(也可以用一个已经赋值的指针去初始化另一个指针)
如果用一个指针变量赋值为0,表示该指针为空指针。
基本用法(举个栗子)

#include<iostream>
using namespace std;
int main()
{
    int num(23);
    int *s=0;
    int *p=0;//(一)声明指针的同时进行初始化赋值
    p=&num;//(二)在声明之后,使用赋值化语句为指针赋值
    s=p;
    int *t=&num;//也可以声明指针变量时用变量地址进行初始化
    cout<<"the integer is:"<<num<<endl;
    cout<<"the num that is pointed to is:"<<*p<<endl;
    cout<<"the address is"<<p<<endl;
    cout<<*s<<endl;
    cout<<*t<<endl;;
    *p=24;//通过访问*p来改变数据的值
    cout<<"the integer is:"<<num<<endl;
    cout<<"the num that is pointed to is:"<<*p<<endl;
    cout<<"the address is"<<p<<endl;
    cout<<*s<<endl;
    cout<<*t;
    return 0;
}

运行结果:

the integer is:23
the num that is pointed to is:23
the address is0x69fef0
23
23
the integer is:24
the num that is pointed to is:24
the address is0x69fef0
24
24

猜你喜欢

转载自blog.csdn.net/qq873044564/article/details/80369233