A "pointer variable" is a variable used to hold the address of a variable. The pointer variable is also a variable, and it differs from other variables in that it does not contain normal data but the address of another variable. There are two ways to access variables, the first is directly based on the variable name to the address of the variable, and then go to the corresponding memory address to get the value of the variable, this method is called "direct reference", the second way is to define a pointer variable to the variable, from the pointer variable to get the address of the variable, then to the memory address to This is called an "indirect reference".
| Second, the application of the pointer |
2.1 How to represent a variable with a pointer
Here are some pictures to see how a variable is represented by a pointer, and it is important to note that the data type of the pointer must be the same as the data type of the variable it points to.
2.2 Arrays and pointers
Arrays can store a set of identical data elements that occupy contiguous memory space (as shown) in memory, so we can traverse or manipulate arrays with pointers.
A palindrome string is a string that is the same as the string, so you can define two pointers, pointing to the first and last characters in the character array, and then pulling backward or forward the paired characters, as shown in the code.
As can be seen from the above example, the characters in the string can be traversed by pointers start++, end--. Arrays are contiguous storage spaces, where each element occupies a byte count of the array data type, so actually the number of bytes moved by the start++ is the number of bytes of the elements in the array, so how does the pointer know the data type of the element? This goes back to the point where the data type of the defined pointer must be the same as the variable data type it points to, in other words, the pointer can only refer to a variable with the same data type as it defines.
2.3 Pointer as parameter & pointer as return value
When we encapsulate a function code in a function, we can use the pointer as a parameter, or we can use the pointer as the return value. Examples are shown in this example.
As shown above, in the main function, the keyboard enters the plaintext into a string, and then calls the encryption (char *) function to complete the encryption. The content to be encrypted is stored in an array, and when the function is called, the array needs to be passed in, which is done with pointers. The PSW passed in when the function is called is the address of the array, received by the pointer P_PSW, called the value of the pointer variable, at this point, it can be said that the pointer p_psw to the first character of the string array. The string is a special array of characters ending with ' s ', so the pointer + + iterates through each character in the string until the pointer points to the '% ' end flag. A pointer to the first address of the reserved string is returned as the return value at the end of the function, and the P_temp returned is the first address of the returned character array. Back to the main function, define a new pointer to receive the return value, in fact, let this new pointer p_new point to the first character of the string, so through puts (p_new) output is the encrypted string.
C language-pointer