C Language (ii) the combination of the increment operator + + and the indirect access operator * and the application Mode

Source: Internet
Author: User

The self-increment operator + + has both prefixes and suffixes, and in conjunction with the indirect access operator *, because of the effects of order, parentheses, and binding relationships, it is easy to misunderstand and produce the wrong results, this article analyzes the different collocation of these operators in detail.

Priority order of + + 、--and *

In the order of precedence of the C language operators, the suffix of + + and-operator precedence 16, the binding relationship is left to right; introduction access operator *, prefix + + and-operator Priority 15, combined with the relationship is right-to-left. Based on this relationship, applications can be analyzed in different situations. For a more intuitive embodiment, there are the following examples.

Examples Show

There are array a[5], values are 10,11,12,13,14 respectively, pointer P is pointed to a[0], and pointer Q and Integer b are also available. Note here that the value of the A array and the P pointer revert to the initial state before each piece of code begins. (at the beginning of writing these test code, ignoring the problem of replying to the initial value, causing many strange errors, such as P no longer point to a[0], or a[0] itself has changed. )

1. Suffix + +

p == p++;

P point to a[1] value is 11,q point to a[0] value is 10. Describes the postfix increment operator, which returns the value before the increment.

2. Prefix + +

p == ++p;

P point to a[1] value is 11,q point to a[1] value is 11. Describes the prefix increment operator, which returns the self-increment value.

3.*p++

p == *p++;

P points to a 11,b value of 10. At this point, the p++,p is performed first, pointing to a[1]. The value of p++ is assigned to B as the value of the pre-increment, that is, the address of a[0], a[0].

4.* (p++)

p == * (p++);

The result is the same as 3. * with the same precedence as the + + operator, the order of operations is from right to left.

5. (*p) + +

p == (*p) + +;

At this point, although the result is the same as 3, but this is the value of the first fetch P point that is a[0],a[0] self-increment that is a[0] the value becomes 11, the increment is the suffix, return the value before the increment, that is 10. The results are the same, but there is a big difference in principle.

6. *++p

p == *++p;

P points to a 11,b value of 11. ++p,p is performed first, the value that points to a[1],++p is the value of the self-increment, that is, the address of a[1], a[1] is assigned to B.

7.* (++P)

p == * (++p);

Same as the 6 result, * with the same precedence as the + + operator, the order of operations is from right to left.

8.++ (*P)

p == + + (*p);

P points to a 11,b value of 11, the value of the first P point to a[0] value is 10,a[0] and the increment to 11, the prefix self-increment return value of 11 is assigned to B.

9. ++*p

p == ++*p;

The result is the same precedence as the 8,* and + + operators, and the order of operations is from right to left.

Summarize

The first principle is that after the prefix value is changed, the suffix value is before the change. The second principle, *++ the same priority, reads from right to left. These are my own try to summarize, if there are errors please correct, I hope to have doubts to help friends.

Full code

/*This program is used to test the prefix increment operator, the suffix increment operator, the operation precedence of the content symbol, and the effect of the return value brought by the order.*/#include"stdio.h"voidMyprint (intNintJintk) {printf ("no.%d:p->%d, q->%d\n", N,j, k);}voidMyprintnew (intNintJintk) {printf ("no.%d:p->%d, b->%d\n", N,j, k);}voidInitinta[5]) {a[0] =Ten; a[1] = One; a[2] = A; a[3] = -; a[4] = -;}//prevents certain tests from changing the values of the array, using the initializer before each use of the arrayintMainintargcChar*argv) {    //int a[5] = {Ten, one, one, one, one}, *p,*q,b;    inta[5], *p, *Q, B;    Init (a); //1.Init (a); P=A; Q= p++; Myprint (1, *p, *q); //P point to 11,q and point to ten//postfix increment operator, which returns the value before the increment//2.Init (a); P=A; Q= ++Q; Myprint (2, *p, *q); //P point to 11,q Point//prefix increment operator, which returns the self-increment value//1, 2: Reading order: From left to right//3.Init (a); P=A; b= *p++; Myprintnew (3,*p, b); //p points to a 11,b value of ten//perform p++,p increment first, point to a[1],p++ value for the value before increment (see 1), Address of a[0], a[0] value assigned to B//4.Init (a); P=A; b= * (p++); Myprintnew (4,*p, b); //The result is the same as 3//* With the same precedence as the + + operator, the order of operations is from right to left//5.Init (a); P=A; b= (*p) + +; Myprintnew (5,*p, b); //The result is the same as 3, but at this point the value of the first P-point is a[0],a[0] the value of a[0] is changed to 11, the increment is the suffix, the value is returned from the pre-increment, that is, ten//with 8 control//6. Init (a); P=A; b= *++p; Myprintnew (6, *p, b); //p points to 11,b values//++p,p is performed first, the value to A[1],++p is the value of the increment (see 2), the address of a[1], and the value of a[1] is assigned to B//7.Init (a); P=A; b= *(++p); Myprintnew (7, *p, b); //Same as 6 results//* With the same precedence as the + + operator, the order of operations is from right to left//8Init (a); P=A; b= ++(*p); Myprintnew (8, *p, b); //p points to 11,b values//first fetch P point a[0] value is 10,a[0] increment to 11, prefix self-increment return value is 11 assigned to B//9??Init (a); P=A; b= ++*p; Myprintnew (9, *p, b); //results with 8//* With the same precedence as the + + operator, the order of operations is from right to left    return 0;}

Run results

C Language (ii) the combination of the increment operator + + and the indirect access operator * and the application Mode

Related Article

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.