C language improvement-pointer expression
Before understanding the pointer expression, you must first define "Left value" and "Right Value". The left value is something that can appear on the left of the value assignment symbol, the right value is something that can appear on the right of the value assignment symbol. Further abstraction can be understood as follows: The left value can be used as an address space to store a value, while the right value can be processed as a value, of course, it should be noted that some of them can be either left or right, for example, defining a as a pointer pointing to B, * a can either be used as the right value, that is, the data stored in address B, or as the left value, that is, B's address. In fact, here we have explained a simple pointer expression, that is: *. Next we will discuss some other pointer expressions.
First, make a statement:
Char ch = "";
Char * cp = & ch;
In fact, this statement declares two variables: ch and cp. cp points to ch as a pointer. For the convenience of subsequent instructions, I now make the following assumptions: The cp variable address is 0x12, and the ch variable address is 0x23, that is, the content in the space cp is: 0x23. The content in ch is a character. The following is an analysis of some expressions:
1. & ch: Right Value: indicates the address information of ch, that is, 0x23. Left value: it is invalid because it cannot be used as a space.
2. cp: the right value. It indicates the content in cp, that is, the address information of ch (0x23 ). Left value: indicates the cp address space, which is used as a space.
3. & cp: the right value indicates the cp address, that is, 0x12. Left value: Analogy & ch also shows that it is invalid as the left value.
4. * cp: This is the same as the example at the beginning of our blog. Right Value: indicates the content in ch (a character ). Left: indicates the address space ch.
5. * cp + 1: note that * (I personally think it is an indirect range operator) has a higher priority than +. Right value: the operation procedure is as follows: perform an indirect access operation to get a copy of Character a, and Add 1 to get character B. Left: the location is not clearly defined and is invalid.
6. * (cp + 1): This expression can regard cp + 1 as a whole. The result of the pointer addition operation is a right value, indicating the next address of ch, * (cp + 1) the right value indicates the data content in the next ch address, and the left value indicates the space of the next ch address (Note: cp + 1 itself is not the left value ).
7. ++ cp and cp ++: this expression involves the ++ operator. Both expressions have the cp + 1 function, the results of the two expressions will also get a copy of the variable, but the prefix ++ will first execute the Add 1 operation and then get the copy of the variable, the suffix ++ is used to copy the variable and then add 1. Therefore, after the ++ cp expression is executed as the right value, both cp and ++ cp will point to the memory location after the cp, after the cp ++ is executed as the right value, the cp will point to the memory location after the cp memory, while the cp ++ overall is directed to the original cp memory location (0x12 ). They do not have clear location definitions, so they are invalid left values.
8. * ++ cp and * cp ++: Compared with the above two expressions, the two expressions are easy to understand. They only perform a pointer operation, that is, indirect access, for the right value, the results of these two expressions are only indirect accesses to the results of the preceding two operations, that is, the data in the corresponding space. For the left value, this is legal, it corresponds to the address of the response address.
Synthesis: in fact, there are still many pointer expressions. The corresponding combinations of different operators form corresponding operators. As long as the corresponding operators are computed step by step based on their priorities, the corresponding results can be obtained. I have described so many expressions, but most of them are general expressions in actual use. The reason why I put them all together is: 1. It helps us to understand others' code, because others may write these expressions. 2. Sometimes it is used, and learning is also necessary. 3. The ultimate goal of summing up so many expressions is not to simply understand the existing expressions, but to fully understand the true meanings of pointers and pointer expressions through the arrangement and understanding of existing expressions, a deep understanding of pointer is helpful for later learning and improvement.