C language Pointer ————— Second article: pointer to another pointer

Source: Internet
Author: User
Tags value of pi

This article turns from: Http://c.biancheng.net/cpp/html/495.html The original part of the problem, has now been modified, and re-issued to review the concept of pointers

As early as the first part of this book I have elaborated on the substance of the pointer. Today we are going to learn a pointer called "pointing to another pointer address." Let's review the concept of pointers first!
When our program declares variables as follows:
short int i;
Char A;
short int * PI;
The program opens up space for each variable in a memory address space, as shown in:

As shown in the figure, you can see:
The i variable occupies 2 bytes at the location of the memory address 5.
The A variable occupies 1 bytes at the location of the memory address 7.
The pi variable occupies 2 bytes at the location of the memory address 9. (Note: Pi is a pointer, I have a pointer width of only 2 bytes, 32-bit system is 4 bytes)
Next, assign the following values:
i = 50;
PI = &i;
After the assignment of the two sentences, the memory image of the variable is as follows:

See no: The short integer pointer variable pi has a value of 5, which is the memory start address of the I variable. So, when we read and write to the *PI, it is actually the read and write operation of the I variable. Such as:
*pi=5; /* is equivalent to i = 5; */
You can go back to chapter II of the book, where there are more detailed explanations.

Second, the address of the pointer and pointer to another pointer address

In the previous section, we saw that the pointer variable itself is in the same memory address as other variables, such as PI's memory start address of 9. Similarly, we may have a pointer pointing to this address. Look at the following code:
short int **ppi; /* This is a pointer to the pointer, note that there are two "*" Number */
PPI = π
First sentence: short int **ppi; --Declares a pointer variable PPI, which is used to store (or refer to) the address of a short int * Type pointer variable.
The second sentence: &pi that is to take the address of pi,PPI = &pi is the address of Pi to the PPI. Assigns an address value of 9 to the PPI. Such as:

It is shown that the content of the PPI of the pointer variable is the starting address of the pointer variable pi. So......
What is the PPI value? --9.
What is the value of *ppi? --5, which is the value of pi.
What is the value of **ppi? --50, which is the value of I, is also the value of *PI.
Oh! I don't have to say too much, I believe you should understand this kind of pointer!

A City application examples

(1) Design a function: void Find1 (char array[], char search, char *pa)
Requirement: The array in this function parameter is a string that ends with a value of 0, requiring that the look-up character in the string array be the character in the parameter search. If found, the function returns the address of the first found character in the array string by a third parameter (PA). If not found, the PA is 0.

Design: According to test instructions, the implementation code is as follows.

voidFind1 (CharArray[],CharSearchChar*PA) {   inti;  for(i =0; * (array + i)! =0; i++)   {      if(* (array+i) = =search) {PA= array +i;  Break; }      Else if(* (array+i) = =0) {PA=0;  Break; }   }}

Do you think this function can achieve the required function?

Debug: I call this function below to try.

intMain () {CharStr[] = {"afsdfsdfdf\0"};/*string to find*/   CharA = ' d ';/*set the character to find*/   Char*p =0;/*If the check is found, the pointer P will point to the address of the 1th character found in the string. */Find1 (str, A, p);/*call the function to implement the action you want. */   if(0==p) {printf ("not Found! \ n");/*if not found, output this sentence*/   }   Else{printf ("found, p =%d", p);/*if found, then output this sentence*/   }   return(0);}


Analysis: What do you think will be the output of the above code? Run the test.
Alas! How to output is: not found! Instead of "found, ...".

Obviously a value is ' d ', and the fourth character of the Str string is ' d ', it should be found!
Look again at the function definition: void Find1 (char array[], char search, char *pa)
See Tuning usefulness: Find1 (str, A, p);

In my analysis of the chapter, the function calls an implicit assignment to each parameter. The entire invocation is as follows:
array = str;
Search = A;
PA = p; */* Please note: The above three sentences are the implied actions at the time of invocation. */
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
}
}
Oh! Parameter PA and parameter search delivery is not different, is the value of the pass it (Small language: address delivery is actually the address value to pass it)! So the modification of the parametric PA value (of course, an address value) does not change the real parametric p value, so the value of P does not change (that is, the point of P is not changed). (If you still have questions, take a look at the C-language pointer ————— First: transfer of function parameters.) )

Correction:

voidFind2 (CharArray[],CharSearchChar**PPA) {   inti;  for(i=0; * (array + i)! =0; i++)   {      if(* (array + i) = =search) {         *ppa = array +i;  Break; }      Else if(* (array + i) = =0)      {         *ppa =0;  Break; }   }}


The main function is changed at the following call:
Find2 (str, A, &p); /* Call the function to implement the action you want. */
Re-analysis: this way the entire operation of calling the function becomes as follows:
array = str;
Search = A;
PPA = &p; */* Please note: The above three sentences are the implied actions at the time of invocation. */
int i;
for (i = 0; * (array + i)! = 0; i++)
{
if (* (array + i) = = search)
{
*ppa = array + i
Break
}
else if (* (array+i) ==0)
{
*ppa=0;
Break
}
}
Did you see that? The PPA points to the address of the pointer p. The modification of *ppa is the modification of P-value. You go to debug yourself.

The modified program will be able to complete the desired function. To understand this example, we have achieved the purpose required by this article.

C language Pointer ————— Second article: pointer to another pointer

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.