Operator priority and combination sequence

Source: Internet
Author: User

Operator priority and combination sequence

I recently saw an opening on the InternetSource codeThere is a linked list operationCode, One row is as follows:

 
-- Pnode-> next;

When I saw this code, I couldn't remember the order in which these operators were combined. So I started searching for information and wrote the following testProgram:

 1  //  Test operator priority  2 Typedef Struct  3   {  4           Int *Next;  5   } Sturcta;  6       7       Int A [] = { 0 , 1 , 2 , 3  };  8       Int B = 4  ;  9 Sturcta temp1 [2  ];  10 Sturcta * ptemp1 = & temp1 [ 0  ];  11       Int Val1 = 0 , Val2 = 0 , Val3 = 0  ;  12       13 Temp1 [ 0 ]. Next = & [ 2 ];  14 Temp1 [ 1 ]. Next = & B;  15       16 Val1 = * -- ptemp1-> next; //  Equals to * (-- (ptemp1-> next); after operation val1 = 1, ptemp1 = & temp1 [0]  17 Nslog ( @"  Val1 value: % d  "  , Val1 );  18      19 Val2 = * -- ptemp1 ++-> next; //  Equals to * (-- (ptemp1-> next), then ptemp1 ++; after operation val2 = 0, ptemp1 = & temp1 [1]  20 Nslog ( @"  Val2 value: % d  "  , Val2 );  21       22 Val3 = * ptemp1-> Next;  23 Nslog ( @" Val3 value: % d  " , Val3 );

This code mainly serves to verify the priority and combination sequence of the plus sign (+), minus sign (--), minus sign (+), minus sign (--), minus sign (-), and minus sign.

Lines 2nd to 5 in the program define a struct, which has a pointer to the int type. Row 9th defines a struct array temp1 [2], followed by a pointer ptemp1 pointing to the 10th elements of the struct array in row 0th. Row 13 and row 14 respectively assign an initial value to the next member variable of the two structs in the array temp1. The last 16 to 23 rows combine the four operators mentioned above and print the test results.

Next we will take a look at the specific combination of several operators:

    1. The first line of code contains the preceding --,-> and * operators. The combination sequence is ptemp1-> next first, then the previous -- operation is performed on the obtained next, and finally the value pointed to by the pointer after the operation is taken out. Based on the analysis, we can get the result of val1 = 1.
    2. 19th lines of code, based on the previous execution(The previous execution has modified the next pointer point)Before --, after ++,-> and * operations. The combination sequence is the plus + operations performed by ptemp1 first(Note: The post ++ operator has the highest priority, but this operation will not be affected)And then obtain the value of next in combination with-> next.(Pointer to int)And then perform the previous -- operation on the pointer, and finally retrieve the value pointed to by the pointer after the -- operation. Based on the analysis, we can get the result of val2 = 0.(Take into account the execution of the 16th rows, ptemp1-> next has pointed to the address of a [1).
    3. 22nd lines of code, based on the previous execution, the post ++ operation of ptemp1 is verified. It can be seen that ptemp1 has pointed to the 1st elements of the struct array.

The above code is only used to verify the priority of operators. We recommend that you add "()" to avoid writing like this in daily Code, especially if you do not want to write like 19 lines. for example, the 19 lines of code can be changed to the following:

 
Val2 = * (-- (ptemp1 ++)-> next ));

In short, if you do not know the priority and the combination order, add more "()", so that you can see clearly, but also the colleagues who want to maintain the code later.

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.