C ++ character arrays and character pointers and strings

Source: Internet
Author: User

The Pointer Points to the address of a variable or class, but why is the character array or string not? Please help me explain why the following code does not show the address, but a string.
Char * Sp = "Example One ";
Char Sa [] = "Two ";
Cout <Sp <& Sa [0];
Why can I output the address after being converted to a string pointer?
String * Ss = new string (Sa); // Sa is the character array above
Cout <Ss;


You have a good question. We know that when a string is being assigned a value, for example, char * p = "hello world"; this assignment is allowed. This does not mean that "hello world" is a pointer. In fact, the compiler has done a lot for us behind the scenes. First, assign a character array in the constant area, then, set the character (including the last '\ 0' character) in the string "hello world .) Copy to this array (in fact, this array can be considered as an anonymous array because there is no array name ). Return the address of the first element of the array and assign the address to the character pointer Variable p. We know that there is no string type in C language, and the string in C language is implemented through the character array, and this character array must have the '\ 0' Terminator. This is where the character string is different from the general character array.


Then again, you asked why char * p = "hello world"; cout <p <endl; printed strings instead of addresses. In fact, I also thought of similar questions and did not find an authoritative answer, but I could guess a few points. The C ++ language is compatible with the C language. Therefore, when outputting a c string, it is output directly with the given first address until the end character '\ 0' ends. Why? I think it should be the characteristics of the language or the characteristics of the compiler. Since the string in C language is implemented through the character array with '\ 0', If you implement the compiler or the printf () function, what will happen if you want to output a string? Are you sure you want to output the pointer address? cout <p <endl; makes the reader clear at a glance, what I want to output is a string, and actually p is a character pointer variable. Why is the output string actually implemented by cout, however, it is certain that cout will indeed act like this when encountering a character pointer: use this pointer as the first address and output the character down until '\ 0' is reached. This is the behavior of cout. In fact, you can write your own cout function. The output address is not based on the default behavior of cout. Of course, to implement the cout function, you must understand a lot of underlying knowledge.

We can output the value of the character pointer in this way, rather than the string with its header address. For example, char a = 'a'; char * p = & A; cout <(int) (p) <endl; you only need to set the pointer value (the address value is actually an integer in the memory and identifies the address of the memory, but the compiler considers the pointer type actually. In essence, any type of computer exists in the memory in binary format, and there is no so-called type. Why is there a type, which is the result of the compiler abstracting the underlying layer ).


Char * z;
Z = "abcd ";
In this Code, the address of a in the string abcd is actually assigned to the pointer z.
When executing z = "abcd", the program first allocates a memory space in the memory for storing the string abcd, then set the pointer z to the first address in the memory space, that is, the address for storing character.

The second question of the landlord ......
Note that the first cout <a <endl; output is not the address of a, but the address of variable B.
* A indicates removing pointer reference, that is, the data stored in the memory address of pointer.
Therefore, * a = & B actually means that the actual memory address of variable B is assigned to the memory address stored by. The result of cout is the memory address of B.



This article is from the "energy of Combustion Technology" blog, please be sure to keep this source http://boyishachang.blog.51cto.com/3485129/1274800

Related Article

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.