C++中*与++组合使用时的优先级

1、*p++,(或者 *(p++)):先解指针,得到数值之后,再将指针p自增;

2、*++p,(或者 *(++p)):指针p先自增,再解指针,得到数值;

3、(*p)++ :先解指针,得到数值,执行完这条语句之后,数值再加一,注意如果是将这个运算输出,那输出值是(*p)的值,执行完之后(*p)的值才增加了1;指针p的指向没有变;

4、++(*p):(*p)的数值自增1;

5、++ *p:先 *p,再将 *p的值增加1;

注意:(*p)++,++(*p)中p最好为int 指针,免得自增或自减出现错误;这两者都不会改变指针p的指向,只会改变p所指向的数据数值,将它们当作普通整数的自增那样看待就行;

#include <iostream>
using namespace std;

int main()
{
	char * stra = "string";
	
	
	cout << "*stra++" << endl;
	while(*stra!='\0')
	{
		cout << *stra++ << endl;
	}
	cout << endl;
	/*
		结果为:
		*stra++
		s
		t
		r
		i
		n
		g 
	*/
	
	
	char * strb ="string";
	cout << "*++strb" << endl;
	while(*strb!='\0')
	{
		cout << *++strb << endl;
	}
	cout << endl;
	
	/*
		结果为:
		*++strb
		t
		r
		i
		n
		g 
	*/
	
	//Õâ¸ö½á¹û¾ÓÈ»ÊǸú*stra++Ò»Ñù£¬Òâ˼ÓÐÎÞÀ¨ºÅ¶¼Ò»Ñù£» 
	char * strc ="string";
	cout << "*(strc++)" << endl;
	while(*strc!='\0')
	{
		cout << *(strc++) << endl;
	}
	cout << endl;
	
	/*
		结果为:
		*(strc++)
		s
		t
		r
		i
		n
		g 
	*/
	
	char * strd ="string";
	cout << "*(++strd)" << endl;
	while(*strd!='\0')
	{
		cout << *(++strd) << endl;
	}
	cout << endl;
	
	/*
		结果为:
		*(++strd)
		t
		r
		i
		n
		g 
	*/
	int stre[] ={1,2,3,4,5};
	int *stre1 = stre;
	cout << "(*stre1)++" << endl;
	cout << (*stre1)++ << endl; //结果为 1
	cout << *stre1 << endl; //结果为 2
	cout << stre[0] << endl;//结果为 2
	cout << endl;

	int strf[] ={1,2,3,4,5};
	int *strf1 = strf;
	cout << "++(*strf1)" << endl;
	cout << ++(*strf1) << endl;//结果为 2
	cout << *strf1 << endl;//结果为 2
	cout << strf[0] << endl;//结果为 2
	cout << endl;
	
	int strg[] ={1,2,3,4,5};
	int *strg1 = strg;
	cout << "++*strg1" << endl;
	cout << ++*strg1 << endl;//结果为 2
	cout << *strg1 << endl;//结果为 2
	cout << strg[0] << endl;//结果为 2
	cout << endl;

	system("pause");
	return 0;
}
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值