Water drop stone wearing C language correctly using const

xiaoxiao2021-03-06  16

Question: Const Variable & Const Limited Content

The following code compiler will report 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 is in P2 .

1) Basic forms used by Const: const char m;

Limited M unality.

2), replace the M, Const char * PM in 1 form;

Limited * PM is not variable, of course, PM is variable, so P1 is pair in the problem.

3) Replace 1 type char, const newtype m;

Limited M unality, Charptr in the problem is a new type, so P2 is not variable, and P2 is wrong.

Question: Const variable & string

What is the problem with the code below?

Char * p = "I'm Hungry!"; p [0] = 'i';

Answer and analysis:

The above code may cause illegal write operations of memory. The analysis is as follows, "I'm Hungry" is essentially a string, and constant is often placed in a read-only memory area and cannot be written. p Initially point to this read-only memory, and P [0] = 'i' attempts to write this place, the compiler will certainly not agree.

Question: Const variable & string constance 2

Can Char A [3] = "ABC" legal? What hidden dangers do it use?

Answer and analysis:

This is legal in standard C, but its living environment is very small; it defines an array of size 3, which is initialized to "ABC", note that it does not have a usual string terminator '/ 0', so this The array only looks like a string in the C language, which is essentially, so all functions for processing the string, such as StrcPy, Printf, etc., can not be used on this false string.

Question 5: Const & Pointer

The type declaration is used to modify a constant, there are two ways to write, then, if you ask, what is the constant content with constant limited?

1), Const in front

Const int nvalue; // nvalue is constconst char * pContent; // * pContent is const, pContent variable const (char *) PContent; // pContent is const, * pContent variable char * const pContent; // pContent is Const * pContent variable const char * const pContent; // pContent and * pContent are constant

2), const is behind, with the above statement

INT const nvalue; // nvalue is constchar const * pContent; // * PContent is const, pContent variable (char *) const pContent; // pContent is const, * PContent variable char * const pContent; // pContent is constant , * PContent variable char const * const pContent; // pContent and * PContent are all constrains and analysis:

CONST and pointers are used together in C language a very common confusion, in actual development, especially when seeing others code, often because it is not good to judge the author's intentions, let's talk about my judgment principle :

Along the * number one line, constell is on the side, then who is const, the constant element is it. You can look at the actual significance of the above declaration according to this rule, and I believe that the order will be at a glance.

In addition, it is necessary to note: For const (char *); because char * is a whole, it is equivalent to a type (such as char), so this is constative pointer to const.

转载请注明原文地址:https://www.9cbs.com/read-44099.html

New Post(0)