C Language Improvement--pointer expression

Source: Internet
Author: User

There is a concept of "lvalue" and "rvalue" before understanding the pointer expression, and for lvalue is something that can appear to the left of the assignment symbol, and the right value is something that can appear to the right of the assignment symbol. Further abstraction can be understood as: Lvalue should be used as an address space to hold a value, and the right value can be treated as a value, of course, it is necessary to explain that some can be both as an lvalue or as an rvalue, for example: Define a as a pointer to B, then * A can be either an rvalue or a data stored in address B, or a left value that represents the address of B. In fact, here is a simple pointer expression, that is: *a. The other pointer expressions are discussed below.


First make a statement:

char ch = "a";

Char *CP = &ch;

In fact, this statement is clear, this declaration declares two variables: ch and CP,CP as a pointer to Ch. For the sake of subsequent explanations, I would now like to assume that the address of the CP variable address is 0x23, which is now in the space CP: the content in 0x23,ch is a character 0x12,ch. Below do some analysis of the expressions:

1, &ch: Right value: Represents the address information of CH, that is, 0x23 this value. Lvalue: It is illegal as an lvalue because it cannot be used as a space.

2. CP: Right value; Indicates the contents of the CP, that is, the address information of CH (0x23). Lvalue: Represents the CP address space, which is used as a space.

3, &CP: Right value: Represents the CP address information, that is, 0x12 this data. Lvalue: Analogy &ch also know that it is illegal as a left value.

4. *CP: This is the same as the example we started with our blog. Rvalue: Indicates the content within CH (a character). Lvalue: Indicates the address space of Ch.

5, *CP + 1: note * (I personally understand that the indirect range operator) has a higher priority than +. Right value: The operation process is: first do the indirect access operation to get a copy of the A character, plus 1, to get the character B. Lvalue: The position is not clearly defined and is illegal.

6, * (CP + 1): This expression can be cp+1 as a whole, as the result of the addition operation of the pointer is an rvalue, representing the next address of CH, * (cp+1) to do the right value for the next address of CH data content, do the left value to represent CH next address this space (note: cp+ 1 is not itself an lvalue).

7, ++CP and cp++: The expression involves the + + operator, two expressions in addition to cp+1 function, the results of two expressions will also be copied, but the prefix + + will perform the operation plus 1 to obtain a copy of the variable, and the suffix + + is the first copy of the variable and then execute plus 1 operation. Therefore, after the ++CP expression is executed as the right value, the CP and ++CP will point to the memory location behind the CP, and the CP will point to the memory location behind the CP memory after cp++ is executed as the right value, while the cp++ overall point to the original memory location of CP (0x12). They don't have a clear location definition, so they're all illegal lvalue values.

8, *++CP and *cp++: Compared to the top two expressions these two are easy to understand, just a pointer operation is an indirect access operation, for the right value, these two expression results are only indirect access to the top two operation results, that is, the data in the corresponding space, for the left value, This time is legal, corresponding to the address of the address information.  

Synthesis: In fact, there are many expressions about pointers, the corresponding combinations between the different operators will form the corresponding operators, as long as the corresponding operators in order to get the priority of the operation can be obtained corresponding results. So many expressions are described, but in practice most of them are general expressions, and the reason they are sorted out is because: 1, it helps us to understand the code of others, because others will probably write these expressions. 2, there are some times are used to get, learning is also necessary. 3, summing up so much of the ultimate goal is not to simply understand the existing expression, but through the existing expression of the collation and understanding, fully understand the pointer and the real meaning of the pointer expression, in-depth understanding of the pointer this thing, to help later learning and improve.

C Language Improvement--pointer expression

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.