C Language -- pointer, pointer

Source: Internet
Author: User

C Language -- pointer, pointer

1. What is a pointer?

"Pointer variable" refers to the variable used to store the variable address. A pointer variable is also a variable. The difference between it and other variables is that it stores not ordinary data, but the address of another variable. There are two ways to access variables. The first is to get the address of the variable directly based on the variable name, and then get the value of the variable from the corresponding memory address. This method is called "direct reference "; the second method is to define a pointer variable pointing to a variable, get the address of the variable from the pointer variable, and then get the value of the variable corresponding to the memory address. This method is called "indirect reference ".

Ii. pointer Application
2.1 how to use a pointer to represent a variable

The following figure shows how to use a pointer to represent a variable. Note that the data type of the pointer must be the same as that of the variable it points.

2.2 arrays and pointers

Arrays can store a group of identical data elements, which occupy continuous memory space (as shown in) in the memory. Therefore, we can traverse or operate arrays through pointers.

The forward and backward reading strings are the same strings. Therefore, two pointers can be defined to point to the first and last characters in the character array, respectively, then, the paired characters are retrieved backward or forward for comparison, as shown in the code.

As shown in the preceding example, the start ++ and end -- pointers can be used to traverse characters in a string. Arrays occupy continuous storage space. Each element occupies the number of bytes of the array data type. In fact, the number of bytes moved by start ++ is the number of bytes occupied by elements in the array, how does the pointer know the data type of an element? As mentioned at the beginning, the Data Type of the defined pointer must be the same as the data type of the variable it points to. In other words, the pointer can only point to the same variable as the data type it defines.

2.3 pointer as parameter & pointer as return value

When we encapsulate a function code into a function, we can use a pointer as a parameter or a pointer as a return value. Example.

 

As shown above, in the main function, the plaintext input by the keyboard is stored in a string, and then the encryption (char *) function is called to complete encryption. The content to be encrypted is stored in an array. When calling a function, you need to pass in the array. Here, the pointer is used. 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 time, we can say that the pointer p_psw points to the first character of the string array. The string is a special character array ending with '\ 0'. Therefore, the pointer ++ is used to traverse each character in the string for encryption until the Pointer Points to the' \ 0' ending sign. At the end of the function, the pointer of the reserved string's first address is returned as the return value. In fact, the returned p_temp is the first address of the returned character array. Return to the main function and define a new pointer to receive the returned value. In fact, this new pointer p_new points to the first character of the string. Therefore, the encrypted string is output through puts (p_new.

 

Related Article

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.