String constants and string Arrays

Source: Internet
Author: User

It is actually the difference between the values of char SZ [] = "string"; and char * psz = "string.

First, let's talk about Char SZ [] = "string"; this statement is stored one by one in the form of arrays. The Compiler interprets it
Char SZ [] = {'s ', 't', 'R',' I ', 'n', 'G','/0 '}; if it appears inside the function, these characters are stored in the stack, so it is not a String constant.

Let's talk about char * psz = "string"; this statement defines a pointer to the "string" string, and there is no space to store the "string" string, obviously, "string" is the most suitable choice to coexist as a String constant in the constant area. And the statement psz [4] = 'X'; no error is reported during compilation and an exception occurs during execution. The reason is that "string" is stored in the read-only storage area and cannot be modified. This is the same as const char * psz = "string" in C ++. Therefore, it is best to add the const keyword to the display so that the compiler can detect the "Modify constant" error.

What should I do if the row parameters in a function are arrays?
For example, void Foo (char SZ [100], int ival [10]);
The answer is to understand it as a pointer:
Void Foo (char * SZ, int * ival );

TheArticleReproduced from the network base camp: http://www.xrss.cn/Dev/C/200711217303.Html

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.