欧卡2中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

需要三步,才能开始

只需两步,慢速开始

欧卡2入门方向盘选莱仕达V9莱仕达折叠便携游戏方向盘支架欢迎地图Mod入驻
查看: 4884|回复: 1
收起左侧

[编程] 表达式*p++该怎么理解

[复制链接]
知行 发表于 2013-10-15 01:20 | 显示全部楼层 |阅读模式
后缀运算符的优先级高于单目运算符,所以理解为 *(p++)
 楼主| 知行 发表于 2013-10-15 01:32 | 显示全部楼层
/* 
 * File:   main.cpp
 * Author: annhe
 *
 * Created on 201年月日, :
 */

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

int main(int argc, char* argv[])
{
    int a[]={1,2,6,4,5};
    int *p=a;
    cout<<"&p :"<<p<<endl;
    cout<<"*p++ : "<<*p++<<endl;
    cout<<"&p :"<<p<<endl;
    cout<<"(*p)++ :"<<(*p)++<<endl;
    cout<<"&p :"<<p<<endl;
    cout<<"*(p++) :"<<*(p++)<<endl;
    cout<<"&p :"<<p<<endl;
    system("pause");
    return 0;
}

运行结果:
$ ./suffix.exe
&p :0x28fed8        //a的地址
*p++ : 1        //p[0]的值,指针右移,同 *(p++)
&p :0x28fedc        //指针右移
(*p)++ :2        //p[1]的值,p[1]加1,变成3
&p :0x28fedc
*(p++) :3        //p[1]的值
&p :0x28fee0        //同*p++,指针右移
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

联系我们|手机版|欧卡2中国 ( 湘ICP备11020288号-1 )

GMT+8, 2024-5-17 19:50 , Processed in 0.048451 second(s), 9 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表