Question about string constants in C Language

Source: Internet
Author: User

Last night I wrote a short C LanguageProgram(In a Linux environment), compilation is successful, but a segment error is always reported during running. I was very depressed becauseCodeNot long. The main function has the following sentence:

Char * STR = "epmzm bpmzm QA eqtt bpmzm qa I EIG ";

The code in the future will operate on this string (the characters in the string are modified ). But changed

Char STR [] = "epmzm bpmzm QA eqtt bpmzm qa I EIG"; then it will run normally.

I thought for a long time and discussed it with my classmates. Finally, I figured out the reason.

The first method is to first create a String constant and point it to it with the STR pointer, which of course has no syntax error. However, a String constant can only be referenced and cannot be modified. Therefore, an error occurs during running.

The second method first creates a character array STR and initializes it. At this time, "epmzm bpmzm QA eqtt bpmzm qa I EIG" is no longer a String constant.

In this way, I come to the conclusion that the constants defined in the C language cannot be modified. Remember

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.