The operation order problem in the combination of pointer and self-increasing operator in C language __c language

Source: Internet
Author: User

To view the original article, please visit: Http://blog.csdn.net/xingjiarong

In C, when the pointer operator and + + or-combined can easily be confused in the order of operations, here is a summary of the following 6 combination: * p++, (* p) ++,* (p++), ++* p,++ (* p), * (++P).

First look at the snippet code and output:

#include <stdio.h> int main () {int a[3]={1,3,5};
    int *p=a;
    printf ("----------------1----------------\ n");
    printf ("%d\n", *p++);
    printf ("%d\n", *p);
    int i;
    for (i=0;i<3;i++) printf ("%d", a[i]);
    printf ("\ n");
    printf ("----------------2----------------\ n");
    P=a;//reset data printf ("%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;//reset data 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;
 }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18--19 20---21 22--23 24 25 26 27 28 29-30 31 32 33 5 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

The output is like this:

The first group: *p++, its order of operation is to return the value of *p first, then P + +.
The second group: (*p) + +, his order of operation is to return the value of *p first, then the value of *p and then + +, this point from the operation of the value of the array a can be seen.
Group III: * (p++), the Order of operations is to return the value of *p, and then p + +, that is, it and *p++ the same order of operations.

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

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

The characteristics of these three groups is the final return *p value, the difference is whether *p first + + or p++.

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.