Pointer

Source: Internet
Author: User

1,

Short int I;
Char;
Short int * pi;

I = 50;
Pi = & I;

---------------- Pointer, strong factory

Short int ** ppi; // This is a pointer to the pointer. Note that there are two * numbers.
Ppi = & pi;

 

------

Result:

The ppi is the IP address of pi.

* The ppi is the content in the pi address value, that is, the I address value. * Ppi (equivalent to pi, with the previous statement "ppi = & pi;" added "*" before, double negation)

** Ppi is the content of I. (Because * ppi = pi, ** ppi = * pi = I)

2. Example 2:

# Include "stdio. h" void find1 (char array [], char search, char * pa) {int I; for (I = 0; * (array + I )! = 0; I ++) {if (* (array + I) = search) {pa = array + I; break;} else if (* (array + I) = 0) {pa = 0; break ;}} int main () {char str [] = {"afsdfsdfdf \ 0 "}; // the string char a = 'd; // you can specify the char * p = 0 character; // if found, the pointer p points to the address of the first character found in the string. Find1 (str, a, p); // call a function to perform the operation. If (0 = p) {printf ("Not Found") ;}else {printf ("found, p = % d", p );}}

 

What do you think is the output of the above code?
Run it.
Alas! The output is: not found!
Instead of: found ,.......
Clearly, the value is 'd', and the fourth character of the str string is 'd'. You can find it!
Let's look at the function definition: void find1 (char [] array, char search, char * pa)
Check the call: find1 (str, a, p );
According to the analysis method in Article 5, an implicit value assignment operation is performed on each parameter during function calling.
The entire call is as follows:
Array = str;
Search =;
Pa = p; // note that the preceding three statements are the action implied during the call. // In my summary, see the function interface section: Exchg2 (int * px, int * py). Note that both the px and py parameters are pointers.
// Check the call position again: Exchg2 (& a, & B) int I; equivalent to px = & a; py = & B;
// It substitutes the address of a (& a) into px, and the address of B (& B) into py. Like the above value transfer, two implicit operations are performed during the function call: The & a, & B values are assigned to px, py.
For (I = 0; * (array + I )! = 0; I ++)
   {
If (* (array + I) = search)
      {
Pa = array + I
Break;
      }
Else if (* (array + I) = 0)
      {
Pa = 0;
Break;
      }
   }
Oh! There is no difference between the parameter pa and the parameter search. It is a value transfer (note: Address transfer is actually an address value transfer )! Therefore, changing the pa value (of course an address value) does not change the p value of the real variable, therefore, the value of p is not changed (that is, the point of p is not changed ).
(If you have any questions, take a look at Article 5: passing function parameters .)

Revised version:

# Include "stdio. h" void find1 (char array [], char search, char ** pa) {int I; for (I = 0; * (array + I )! = 0; I ++) {if (* (array + I) = search) {* pa = array + I; break;} else if (* (array + I) = 0) {* pa = 0; break ;}} int main () {char str [] = {"afsdfsdfdf \ 0 "}; // the string char a = 'd; // you can specify the char * p = 0 character; // if found, the pointer p points to the address of the first character found in the string. Find1 (str, a, & p); // call a function to perform the operation. If (0 = p) {printf ("Not Found");} else {printf ("found, p = % d", * p );}}

 

Complete analysis version:

# Include "stdio. h "void find1 (char array [], char search, char * pa) {int I; // note that not * pa = & p; but pa = & p, * pa = p. // If the preceding parameter is * pa, then pa = p is only a copy of p. pa is the content of p, so * pa points to the address where the content is used, the content is unknown. for (I = 0; * (array + I )! = 0; I ++) {if (* (array + I) = search) {* pa = array + I; break;} else if (* (array + I) = 0) {* pa = 0; break ;}} int main () {char str [] = {"afsdfsdfdf \ 0 "}; // the string char a = 'd; // you can specify the char * p = 0 character; // if found, the pointer p points to the address of the first character found in the string. Find1 (str, a, & p); // call a function to perform the operation. If (0 = p) {printf ("Not Found");} else {printf ("found, p = % d", * p );}}

 

Reference: http://blog.pfan.cn/whyhappy/5790.html

Pointer

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.