Complete the C pointer-pointer to another pointer

Source: Internet
Author: User
Tags value of pi

 

I. Review pointer concepts:
As early as in the second part of this series, I have elaborated on the essence of pointers. Today, we need to learn a pointer that points to another pointer address. Let's review the pointer concept first!
When our program declares the following variables:
Short int I;
Char;
Short int * PI;
The program will open up space for each variable in an address space in the memory, as shown in.
Memory Address → 6 7 8 9 10 11 12 13 14 15
Bytes -------------------------------------------------------------------------------------
... |
Bytes -------------------------------------------------------------------------------------
| Short int I | char a | short int * PI |
As shown in the figure, we can see:
The I variable occupies two bytes at the memory address 5.
A variable occupies one byte at the location of memory address 7.
The PI variable occupies two bytes at the memory address 9. (Note: PI is a pointer. Here, the pointer width is only two bytes, and the 32-bit system is four bytes)
Next, assign values as follows:
I = 50;
Pi = & I;
After the assignment in the preceding two sentences, the memory image of the variable is as follows:
Memory Address → 6 7 8 9 10 11 12 13 14 15
Bytes --------------------------------------------------------------------------------------
... | 50 | 6 |
Bytes --------------------------------------------------------------------------------------
| Short int I | char a | short int * PI |
No: the PI value of the Short integer pointer variable is 6, which is the memory start address of the I variable. Therefore, when we perform read/write operations on * Pi, it is actually a read/write operation on the I variable. For example:
* Pi = 5; // It is equivalent to I = 5;
You can go back to the second article in this series for more detailed explanations.

2. pointer address and pointer pointing to another pointer address
In the previous section, we can see that the pointer variable itself is also in a memory address like other variables, for example, the memory start address of PI is 10. Similarly, we may also point a pointer to this address.
See the following code:
Short int ** PPI; // This is a pointer to the pointer. Note that there are two * numbers.
PPI = & PI;

First sentence: Short int ** PPI; -- declares a pointer variable PPI, which is used to store (or point to) the address of a short int * type pointer variable.
The second sentence: & Pi is the PI address, and PPI = & PI; is to assign the PI address to the PPI. Assign the address value 10 to the PPI. For example:
Memory Address → 6 7 8 9 10 11 12 13 14 15
Bytes ------------------------------------------------------------------------------------
... | 50 | 6 | 10 |
Bytes ------------------------------------------------------------------------------------
| Short int I | char a | short int * PI | short int ** PPI |
We can see that the starting address of the pointer variable PI is the content of the pointer variable PPI. So ......
What is the PPI value? -- 10.
* What is the PPI value? -- 6, that is, the value of pi.
** What is the PPI value? -- 50, that is, the value of I, is also the value of * pi.
Haha! I don't need to say too much. I believe you should understand this pointer!

3. One application instance
1. Design a function: void find1 (char array [], char search, char * PI)
Requirements:The array in this function parameter is a string ending with a 0 value. It is required that the character in the string array be the character in the search parameter. If found, the function returns the address of the first character found in the array string through the third parameter (PA. If not found, PA is 0.
Design:The implementation code is as follows.
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;
}
}
}
Do you think this function can implement the required functions?
Debugging:
Let me call this function.
Void main ()
{
Char STR [] = {"afsdfsdfdf/0"}; // string to be searched
Char A = 'd; // set the character to be searched
Char * p = 0; // 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! /N "); // 1. Output this sentence if not found
}
Else
{
Printf ("found, P = % d", P); // output this sentence if found
}
}
Analysis:
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.
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! 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 .)
Fixed:
Void find2 (char [] array, char search, char ** PPA)
{
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;
}
}
}
The main function is called as follows:
Find2 (STR, A, & P); // call a function to perform the operation.
Further analysis:
In this way, the entire operation is changed to the following:
Array = STR;
Search =;
PPA = & P; // Note: The above three statements are the Action implied during the call.
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;
}
}
Do you understand?
The PPA points to the IP address of the pointer p.
The modification to * PPA is the modification to the P value.
Debug it on your own.
The modified program can complete the required functions.

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.