The meaning of the array of character pointers and pointers to pointers in C + +

Source: Internet
Author: User
Tags first string function prototype

Pointing to the pointer, a long time ago, said the pointer, but later found that a lot of people are still more difficult to understand, this time we carefully said again to point to the pointer.

Look at the following code and note the annotations in the code:

 #include <iostream> 
#include <string>
using namespace std;

void Print_char (char* array[],int len);//function prototype declaration
Void Main (void)
{
//----------------------------- Segment 1-----------------------------------------
Char *a[]={"abc", "CDE", "FGH"};//character pointer array
char* *b=a;// Defines a pointer to a pointer and assigns the address of the first string pointed to by the first address of the pointer array to the ABC\0 string's first address
cout<<*b<< | <<* (b+1) << "|" <<* (b+2) <<endl;
//-------------------------------------------------------------------------
//-------------------------- ---segment 2-----------------------------------------
char* test[]={"abc", "CDE", "FGH"};//Note that this is a quote, a string, The next address is plus 4 bits (on 32-bit systems)
int num=sizeof (test)/sizeof (char*);//calculation of the number of strings
Print_char (test,num);
Cin.get ();
//-------------------------------------------------------------------------
}

void Print_char ( char* array[],int len)//When the call is passed in is not an array, but a character pointer he adds 1 to each plus sizeof (char*) length
{
for (int i=0;i<len;i++)
{
cout<<*array++<<endl;
}
}

Let's take a closer look at the array of character pointers and pointers to pointers, and the program in paragraph 1 looks like this:

Char *a[]={"abc", "CDE", "FGH"};
char* *b=a;
cout<<*b<< "|" <<* (b+1) << "|" <<* (b+2) <<endl;

Char *a[] Defines an array of pointers, noting that it is not char[], char[] is not initialized to three characters at the same time, the definition of a later a[] in fact, there are three memory locations, respectively, storing the ABC,CDE,FGH, three string of the starting address, The memory addresses in these three locations are not the starting addresses for these three strings, in this example a[] is stored in the stack space, and three strings are stored in the static memory space in the const region, and then we see the char* *b=a; here is a pointer to the pointer, If you write char *b=a, then it is wrong, because the compiler returns an error that cannot convert char* *[3] to char *, and the b=a assignment, in effect, assigns the first address of A to B, since B is a pointer to the pointer, and the output of the program is COUT<<*B << "|" <<* (b+1) << "|" <<* (b+2) <<endl;

The result is

abc
cde
fgh

You can see that the +1 operation of each memory address is in fact a plus sizeof (char*) operation, we in 32-bit system sizeof (char*) length is 4, so each plus 1 is +4, is actually *a[] three position +1, so * (b+1) The result is the CDE, and we may ask, why is the output a CDE instead of a C? The answer is this, in C + +, the output character pointer is the output string, the program automatically stops after encountering.

We finally analyze the code in paragraph 2, and in paragraph 2 we call the Print_array () function, in which the formal argument is char *array[] and the char *test[in the code, the same as the character pointer, when you pass the argument over, In fact, instead of passing the contents of the array, the first address of test is passed in, because the array is the pointer, so in memory it is in the stack, has the same properties as the variable, can be the left value, so our output is written as,cout<<*array++<<endl; Of course we can also rewrite for Cout<<array[i]<<endl, here in the loop every time plus 1 operation and paragraph 1 code The General truth is the same, pay attention to see the following figure!

Here are the two very important points of knowledge we have said, to say, to a thorough understanding of the hope that the reader more hands-on, more observation, practice makes perfect.

The following is a schematic diagram of 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.