For some reason, C has been reorganized. The past year is also a waste. Fortunately, it can be retried. I learned the pointer section in C and pointer three or four times, and then I did the exercises later. Now let's take a look back. Right and wrong are not too many comments.
Pointer is indeed one of the characteristics of C, and it has not been clearly understood from the very beginning. It is said that there are not many people who really thoroughly understand it. This is just my study notes. Pointer, remember what the C language teacher said at the beginning, that is, the address. Nothing else will be impressed. The pointer is the address, and the value used to access the corresponding memory space is indirect access. The pointer should be '*', also called the unreference pointer.
Through the reference books C and pointers, I learned that we use variables to replace addresses, that is, variables in our ports. The hardware still accesses the memory location through the address. The Value Type of the corresponding memory space storage is not an inherent feature of the value, but depends on its usage, which is an implicit decision.
Before using a pointer, you must initialize it through the & operator. Otherwise, it should be initialized as a null pointer, that is, null. The advantage of this is to prevent unknown errors caused by pointer initialization.
Note that the value of a variable is the value stored in the memory location assigned to the variable, even if the pointer variable is no exception. For example
Int A; // assume that the address of a is 100int * B; A = 120; B = & A; printf ("% d"; A, B );The resulting values are 120 and 100. At this time, we should also remember that the value of the pointer variable is a number. Unless there is an indirect access operator, we will not be able to access the corresponding memory space value.
Before resolving the reference pointer, we must make sure that the corresponding pointer is not a null pointer because it cannot be indirectly accessed. Otherwise, the program will be terminated incorrectly or the access memory location will be zero, leading to unknown errors.
(Check the validity of parameters)
Pointer constants are rarely used, except in extremely special cases (you can determine the memory location of the data ). Note that if you know the exact position of the pointer variable, that is, an integer value, you should force type conversion through its indirect access operations. For example
*(int*)100 =5;
The value 5 is stored at the memory location of 100.
The pointer should first understand a pointer, read more books, do exercises, and be more profound in terms of concept.
Some commonly used pointer expressions are encountered during learning. For example
char cp = 'a';char *cp =&ch;
& Ch; CP; // left value of the right value & CP; * CP; // <span style = "font-family: Arial, Helvetica, sans-serif; "> left value of the right value </span> * CP + 1; * (CP + 1); // left value of the right value + + CP; CP ++; * ++ CP; // The left value of the right value * CP ++; // The left value of the right value + + * CP; (* CP) ++; // ++ * ++ CP; // ++ * CP ++;
The value not marked above is valid only when it is the right value.
However, as the author of the book says, this is not to let everyone remember, the key is to let everyone understand the meaning of these expressions.
Frankly speaking, I am confused about these expressions, but I forgot to refer to the Manual for a long time.
In addition, for pointer operations, when the pointer and an integer perform arithmetic operations, the pointer value is adjusted according to the appropriate size. That is, the pointer variation is equal to the integer multiplied by the size of the byte occupied by the Data Type (char 1, short 2, int 4, double 8 ).
Only the pointer pointing to the same array can be used for subtraction. The result of the subtraction operation has nothing to do with the data type. The value of the subtraction operation is the distance between two pointers in the memory (in units of the length of the array element, rather than in bytes), because the result of the subtraction operation is divided by the length of the array element type. For example, if P1 points to array [I] and P2 points to array [J], the value of the p1-p2 is the value of J-I.
Study Notes: pointer C