C character pointer access

Source: Internet
Author: User

Because of an interview question, I caught tan haoqiang's C LanguageProgramIn the design section, in the 240-page 10.22 examples, the character Pointer Points to a character, which can be accessed through a character array, using [], that is: char * A = "I love China ";

Printf ("The Sixth charcter is % C \ n", a [5]);

I suddenly wondered if I could use a character pointer to change the content that it points to, that is

In the vs2005 environment, "0xc0000005: Access conflict occurs when the write location is 0x00418bf0". If it is written as follows, it can be executed normally.

That is to say, it is allowed to "write" the character array through the character pointer, and the previous error is a running error, similar to the exc_bad_access error in xcode,The possible cause of speculation is that the character pointer is specified and the string is initialized directly. The memory space opened up is not the space on the stack.Const pointer errors. If "char * A =" I love China ";" is used, it is written as "const char * A =" I love China "; "To compile the" write operation"CodeYes, the compiler will report an error. Meanwhile, ask "char * A =" I
Love China ";" memory space.

Here is the answer to this question http://blog.csdn.net/defonds/article/details/6260429

"Therefore, the string literal in C ++ can be called a String constant, and the first address and each element address are the same as those in C. They are all address constant expressions.
the string literal is of the array type char [N] in C and const char [N] in C ++. in an expression, when an array is converted to a pointer, the corresponding equivalent pointer types are char * and const char *. Therefore, in C, char * P = "abcdef" is legal, but it is surprising that the preceding statement is also legal in C ++! It seems that a pointer to const char pointer is given a pointer to Char pointer, which seems to be in violation of the more CV-qualified principle for pointer conversion in C ++. In fact, there are two types of conversions in
C ++: one is based on the current context, and the other is based on the conversion from array to pointer, c ++ standard content: "

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.