The problem of operation order in the combination of pointer and increment operator in C language

Source: Internet
Author: User

In the C language, when the pointer operator and + + or – combine to easily divide the order of operations, summarized here, the following is a total of analysis 6 combinations: * p++, (* p) ++,* (p++), ++* p,++ (* p), * (++P).

First look at the segment code and output:

#include <stdio.h>intMain () {inta[3]={1,3,5};int *p=a;printf("----------------1----------------\ n");printf("%d\ n",*p++);printf("%d\ n",*p);intI for(i=0;i<3; i++)printf("%d ", A[i]);printf("\ n");printf("----------------2----------------\ n"); P=a;//resetDataprintf("%d\ n",(*p)++);printf("%d\ n",*p); for(i=0;i<3; i++)printf("%d ", A[i]);printf("\ n");printf("----------------3----------------\ n"); a[0]=1;//resetData p=a;printf("%d\ n",*(p++));printf("%d\ n",*p); for(i=0;i<3; i++)printf("%d ", A[i]);printf("\ n");printf("----------------4----------------\ n"); P=a;printf("%d\ n",++*p);printf("%d\ n",*p); for(i=0;i<3; i++)printf("%d ", A[i]);printf("\ n");printf("----------------5----------------\ n");    P=a; a[0]=1;printf("%d\ n",++(*p));printf("%d\ n",*p); for(i=0;i<3; i++)printf("%d ", A[i]);printf("\ n");printf("----------------6----------------\ n");    P=a; a[0]=1;printf("%d\ n",*(++P));printf("%d\ n",*p); for(i=0;i<3; i++)printf("%d ", A[i]);printf("\ n");return 0;}

The output is this:

The first group: *p++, whose order of operations is to return the value of *p first, and then P + +.
Second group: (*p) + +, his order of operations is to return the value of *p first, and then the value of *p + +, which is seen from the value of array a after the operation.
The third group: * (p++), the Order of operations is to return the value of *p first, and then P + +, that is, it is the same as the *p++ order of operations.

All three groups return the value of *p first, the difference is whether it is the value of p++ or *p + +.

Group Fourth: ++*p, the value of *p is first + +, and then the value of *p is returned.
Group Fifth: + + (*p), the value of *p is first + +, and then return the value of *p, so it is the same as the + + *p.
Group Sixth: * (++P), first the value of P + +, and then return the value of * p, and *++p is equivalent.

The three groups are characterized by the final return of the value of *p, the difference is whether it is *p first + + or p++.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The problem of operation order in the combination of pointer and increment operator in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.