Const, a read-only variable in C Language

Source: Internet
Author: User
Tags array definition

Const, a read-only variable in C Language

Read-Only variable, so that the value of the variable cannot be changed.
Const is a key word in C language. It specifies that a variable cannot be changed. Using const can improve the robustness of the program to a certain extent. In addition, while watching other people's code, you can clearly understand the role of const and help you understand the program of the other party.

Although it sounds very simple, in fact, the use of const is also a subtle place in C language, where is the subtlety? See the following questions.

Problem: const variable & Constant

Why does the ansi c compiler report an error when I use a const variable to initialize an array like in the following example?

Const int n = 5;

Int A [n];

Answer and analysis:

1) the difference between "constant" and "Read-Only variable" is discussed. Constants must be read-only, for example, 5, "ABC", etc. They must be read-only, because there is no place in the program to store its value, and of course it cannot be modified. The "Read-Only variable" is to open a place in the memory to store its value, but this value is limited by the compiler and cannot be modified. The const keyword in C language is used to restrict a modifier that cannot be changed ). In the above Code, variable N is modified as a read-only variable. Unfortunately, the modification is not a constant. Ansi c requires that the dimension of an array definition must be "constant" and "Read-Only variables" are not allowed.

2) Note: in ansi c, this writing method is incorrect, because the array size should be a constant, while const int N and n are just a variable (constant! = Immutable variables, but in the Standard C ++, this defines a constant, which is correct). In fact, according to the compilation process and memory allocation, this method should have been reasonable, but ansi c imposes restrictions on arrays.

3) So what are constants defined in ansi c? The answer is the enum type and the # define macro, both of which can be used to define constants.

Question: content defined by the const variable & Const

The following code compiler reports an error. Which statement is wrong?

Typedef char * pstr;

Char string [4] = "ABC ";

Const char * P1 = string;

Const pstr P2 = string;

P1 ++;

P2 ++;

Answer and analysis:

The problem lies in P2 ++.

1) The basic const format: const char m;

The limit m is unchangeable.

2) Replace m and const char * PM in Formula 1;

The limit * PM is unchangeable. Of course PM is variable. Therefore, P1 ++ is correct in the problem.

3) Replace 1 char, const newtype m;

The limit m is immutable, and the charptr in the problem is a new type. Therefore, P2 is immutable in the problem, and P2 ++ is incorrect.

Problem: const variable & String constant

What is the problem with the following code?

Char * P = "I'm hungry! ";

P [0] = 'I ';

Answer and analysis:

The code above may cause invalid memory write operations. The analysis is as follows: "I'm hungry" is essentially a String constant, while constants are often placed in the read-only memory area by the compiler and cannot be written. P initially points to this read-only memory zone, while P [0] = 'I' attempts to write this place, And the compiler certainly won't agree.

Problem: const variable & String constant 2

Is char a [3] = "ABC" Legal? What are the risks of using it?

Answer and analysis:

This is legal in Standard C, but its living environment is very small; it defines an array of 3, initialized as "ABC", note, it does not have the usual string Terminator '\ 0', so this array only looks like a string in C language, but it is not actually, so all functions that process the string, for example, strcpy and printf cannot be used on this fake string.

Question 5: const & pointer

In the type declaration, const is used to modify a constant. There are two ways to write it. So, what are the following statements that use const to limit immutable content?

1) const is in front

Const int nvalue; // nvalue is const

Const char * Pcontent; // * Pcontent is const, and Pcontent is variable

Const (char *) Pcontent; // Pcontent is const, * Pcontent is variable

Char * const Pcontent; // Pcontent is const, * Pcontent is variable

Const char * const Pcontent; // both Pcontent and * Pcontent are const

2) const is behind and equal to the above statement

Int const nvalue; // nvalue is const

Char const * Pcontent; // * Pcontent is const, and Pcontent is variable

(Char *) const Pcontent; // Pcontent is const, * Pcontent is variable

Char * const Pcontent; // Pcontent is const, * Pcontent is variable

Char const * const Pcontent; // both Pcontent and * Pcontent are const

Answer and analysis:

Using const and pointer together is a common confusion in C language. In actual development, especially when looking at other people's code, it is often difficult to judge the author's intention because of this, the following describes my judgment principles:

Draw a line along the line *. If the const is on one side, then the const is the element limited by the const. You can view the actual meaning of the above statement based on this rule, and I believe it will be clear at a glance.

In addition, NOTE: For const (char *); Because char * is a whole, equivalent to a type (such as char), this is to limit the pointer to const.

Const has nothing to do with the scope. It tells the compiler that the value cannot be modified after const is defined.

As for putting it in the global, it naturally has a global scope.

Http://hi.baidu.com/%BF%DE%C6%FC%B5%C4%D6%ED11/blog/item/c235b18aa2c882799e2fb43e.html

Http://www.cnblogs.com/JCSU/articles/1299051.html C language explanation-Enumeration type

Http://www.360doc.com/content/11/0325/11/6605519_104441449.shtml

Http://www.cppblog.com/elva/archive/2011/01/26/139322.html C language constant

Definition of C language constants in http://c.chinaitlab.com/basic/750331.html

Http://www.ttlearn.net/html/28/n-328.html C language constants and variables

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.