c指针的基础使用回顾一

指针的基础使用回顾一
#include <iostream>

/**指针的基础使用回顾一*/
using namespace std;

//交换函数里的*才真正访问到了a,b的空间,并对其空间里的值进行了交换操作
//程序①
void exchange(int *a,int *b)
{
    int c;
    c=*a;
    *a=*b;
    *b=c;
    //交换了a和b的地址
}

int main()
{
    int a,b;
    a=5,b=6;
    cout<<a<<'\t'<<b<<endl;
    cout<<&a<<'\t'<<&b<<endl;
    exchange(&a,&b);
    cout<<a<<'\t'<<b<<endl;
    cout<<&a<<'\t'<<&b<<endl;


//仔细看以下交换程序
//分析运行结果可知,交换的地址实际上也是副本,故实际地址值并没有交换
//程序②
//void exchange(int *a,int *b)
//{
//    int *c;
//    c=a;
//    a=b;
//    b=c;
//    //交换了a和b的地址
//}
//
//int main()
//{
//    int a,b;
//    a=5,b=6;
//    cout<<a<<'\t'<<b<<endl;
//    cout<<&a<<'\t'<<&b<<endl;
//    exchange(&a,&b);
//    cout<<a<<'\t'<<b<<endl;
//    cout<<&a<<'\t'<<&b<<endl;

//程序③
//int main()
//{
//    int a=1;
//    int *p=&a;
//    int *ptr=NULL;
//    cout<<a<<endl;
//    cout<<"a的地址是:"<<&a<<endl;
//    cout<<"指针的值是:"<<p<<endl;
//    cout<<"指针所指向空间的值是:"<<*p<<endl;
//    cout<<"指针的指针是:"<<&p<<endl;
//    //空指针所指空间的值:错误
//    //cout<<"ptr所指向空间的值是:"<<*ptr<<endl;
//    cout<<"ptr的值:"<<ptr<<endl;
//    cout<<"ptr指针的指针是:"<<&ptr<<endl;
//    cout<<"int类型所占空间为:"<<sizeof(int)<<endl;
    return 0;
}

运行对比结果

程序①:

程序②:

程序③:

(ps:对比截图会对指针更加了解)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值