C + + pointers summary function parameter pointer invocation and stack memory allocation principle

Source: Internet
Author: User

A char pointer in C + +

This char pointer is interesting, and the char pointer usually has two types of initialization. One is initialized with a char array, and one is initialized with a char variable.

String constants in C + + that use double quotation marks to start a string that has been initialized by the compiler to a const char[] type. In other words, "hedd" is actually assigning a const to an address in memory when assigned to another variable or constant. The head pointer of the char array is assigned to it. If you use a number such as ' s ', you cannot assign a value directly to a char pointer, because ' s ' is a character variable or constant that has memory. This situation must use a char variable or a const Char requests memory in memory and initializes it to a character, and then assigns a value to the CHA pointer using this variable or constant.

Output the data pointed to by the char pointer using cout or printf

When using cout or printf to output a char pointer, their policy is that, if it is a char pointer, the data is read from the first memory region pointed to by the pointer, and the data is read backwards until the ' \ S ' end character is obtained.

So I heard the cout output char pointer to the address is unrealistic, you must take the char pointer to another pointer, generally I will use the conversion to void pointer. This translates into an opportunity to output the memory block address that the char pointer points to.

cout and printf only have such a unique judgement on the character pointer, for other types of pointers will directly output the address of the pointer, and will not read the contents of the address, and will not continue to read the contents of the next address until the end of the "\" character.

From what we can find, cout to the char pointer using the * value or normal, he will only go to the address that is stored in the data. Instead of automatically reading the contents of the next address

From what we can find, when using a char variable address to a char pointer, and then using the cout output, cout automatically reads the contents of the address after the char variable until a content conversion is found to the address of char equal to ' s '. In this way there is a case of cross-border reading.

Now we look back and see why using cout to read char i[6]={' h ', ' e ', ' l ', ' l ', ' s '} and ' Hello ' and the const char*p= ' Hello ' results are the same. We also found that compilers are allowing us to use " Hello "to the const char J[6] Array is directly assigned value.

C + + pointers summary function parameter pointer invocation and stack memory allocation principle

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.