The pointer pointing to the pointer was said long ago, but later it was found that many people are still hard to understand. This time, let's talk about the pointer pointing to the pointer again!
Let's take a look at the following code. Pay attention to the annotations in the code!
// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream>
# Include <string>
Usingnamespacestd;
Voidprint_char (char * array [], intlen); // original function declaration
Voidmain (void)
{
// ----------------------------- Segment 1 -----------------------------------------
Char * a [] = {"abc", "cde", "fgh"}; // character pointer Array
Char ** B = a; // defines a pointer to the pointer, and assigns the address of the first string pointed to by the first address of the pointer array, that is, the first address of the abc string.
Cout <* B <"|" <* (B + 1) <"|" <* (B + 2) < //-------------------------------------------------------------------------
// ----------------------------- Section 2 -----------------------------------------
Char * test [] = {"abc", "cde", "fgh"}; // note that the quotation marks indicate a string, the next address plus 1 is plus 4 bits (on a 32-bit System)
Intnum = sizeof (test)/sizeof (char *); // calculate the number of strings
Print_char (test, num );
Cin. get ();
//-------------------------------------------------------------------------
}
Voidprint_char (char * array [], intlen) // when the call is made, it is not an array, but a character pointer. Each value of 1 is the length of sizeof (char *).
{
For (inti = 0; I {
Cout <* array ++ < }
}
Next we will explain the character pointer array and pointer to the pointer carefully. The program in section 1 is as follows:
char*a[]={"abc","cde","fgh"};
char* *b=a;
cout<<*b<<"|"<<*(b+1)<<"|"<<*(b+2)<
Char * a [] defines a pointer array. Note that not char [], char [] cannot be initialized to three characters at the same time, after definition, a [] Actually has three internal memory locations, respectively storing abc, cde, fgh, and the starting address of the three strings, the memory addresses in these three locations are not the starting addresses of these three strings. In this example, a [] is stored in the stack space, the three strings are stored in the const region of the static memory space. Next we will see char ** B = a; here we define a pointer to the pointer, if you write char * B = a; then it is incorrect, because the compiler will return an error that cannot convert char ** [3] To char *. B = a value assignment, actually, the first address of a is assigned to B. Because B is a pointer to a pointer, program output cout <* B <"|" <* (B + 1) <"|" <* (B + 2) < The result is
Abc
Cde
Fgh
We can see that each memory address + 1 operation is actually a sizeof (char *) operation. In a 32-bit system, the length of sizeof (char *) is 4, therefore, each addition of 1, that is, + 4, is actually the + 1 in the three internal positions of * a []. Therefore, the result of * (B + 1) is naturally cde, at this time, we may ask why the output is cde rather than c? The answer is: in c ++, the output character pointer is the output string, and the program stops automatically when it encounters it.
Finally, let's analyze the code in Section 2. In section 2, we call the print_array () function, the formal parameter in this function is the same as char * array [] and char * test [] in the Code. It is also a character pointer. When you pass the parameter, in fact, it is not to pass the array content. The first address of test is passed in. Because array is a pointer, it has the same properties as variables in the memory in the stack area, the value can be left, so the output is written as cout <* array ++ < We have finished these two very important knowledge points here. To put it bluntly, we hope that readers can learn more and observe more!
420) {this. width = 420} ">
Memory Structure!
Back to Guan Ning's C ++ tutorial topic