Example analysis based on C++ output pointer self increment of ++ operation

  • 2020-04-02 00:55:14
  • OfStack


#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
 char s[] = "012345678", *p = s;

 cout << "s:"<<s<<endl;
 cout << "*p++ = " << *p++ << ", *(p++) = " << *(p++) << ", (*p)++ = " << (*p)++ << ", *++p = " << *++p << ", *(++p) = "<< *(++p) << ", ++*p = " << ++*p << ", ++(*p) = "<< ++(*p) << endl;
 cout<<"-------------------"<<endl;

 char s1[] = "012345678";
 p = s1;
 cout << endl << "s1:"<<s1<<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;
 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;
 cout << "++(*p) = " << ++(*p) <<endl;
 cout<<"-------------------"<<endl;
 system("pause");
 return 0;
}

Output:
  S: 012345678
* p++ = 3, * (p++) = 3, + = (* p) + 2, + + p = 4 and * (+ + p) = 4, + + * p = 4, + + (* p) = 4
-------------------
S1:012345678
* p         = 0
* p++     = 0
* p         = 1
* (p++) = 1
* p         = 2
(* p) + + = 2
* p         = 3
* + + p     = 3
* p         = 3
* (+ + p) = 4
* p         = 4
+ + * p     = 5
* p         = 5
+ + 6 = (* p)
-------------------
Please press any key to continue..

Related articles: