The character pointer array in C/C ++ and the pointer to the pointer

Source: Internet
Author: User

The pointer pointing to the pointer was said long ago, but later I found it hard for many people to understand it. This time we will go over the pointer pointing to the pointer again.

Take a look at the following code and pay attention to the annotations in the Code:

# Include <iostream>
# Include <string>
Using namespace STD;
 
Void print_char (char * array [], int Len); // original function declaration
 
Void main (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/0 string.
Cout <* B <"|" <* (B + 1) <"|" <* (B + 2) <Endl;
//-------------------------------------------------------------------------
 
// ----------------------------- 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)
Int num = sizeof (TEST)/sizeof (char *); // calculate the number of strings
Print_char (test, num );
Cin. Get ();
//-------------------------------------------------------------------------
}
 
Void print_char (char * array [], int Len) // when the call is made, it is not an array, but a character pointer. Each value of 1 is added with sizeof (char *) length
{
For (INT I = 0; I <Len; I ++)
{
Cout <* array ++ <Endl;
}
}

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 =;
Cout <* B <"|" <* (B + 1) <"|" <* (B + 2) <Endl;

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: ABC/0, CDE/0, fgh/0, 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) <Endl;

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 after/0.

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 we have written the output, cout <* array ++ <Endl; of course, we can also rewrite it to cout <array [I] <Endl, here, the general principle of adding 1 to each operation in the loop is the same as that of Section 1 code. Pay attention to the figure below!

We have finished these two very important knowledge points here. To put it bluntly, we hope that readers can learn more, observe more, and make perfect use of them.

Below is the memory structure:

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.