Keywords on C - Constance and Summary

xiaoxiao2021-04-09  466

Keywords on C - Constance and Usage

The usage in C is flexible (I believe in C ), sometimes const is very easy, and often because of its advantages, the reason for making mistakes is more important, and another more important, It is not in place for Const. So I wrote itself into a small summary. Written is very beautiful.

I wrote a nonsense above, even if it is a small order, ^ _ ^ Next is the text;

First, the specific definition of Const:

- It feels difficult to define it next standard, because the usage is flexible, it seems that it will never understand it after it is defined, and it is easy to make misunderstand (even the level is too delicious). For example, if it defines it: A keyword that allows variables to become a constant that cannot be modified. So, in this case, it may make people misunderstand that as long as the const is in the defined variable, that variable cannot be modified no matter how it is. Such an understanding is a comparison (discusses to this problem). Therefore, I don't dare to define it, other reference books (I have seen) seems to be defined.

Second, the specific role of Const

- Const effect is flexible, and the location of Const placed in an expression may not be the same. The following detailed analysis (of course, the situation is not covered)

A, const most often usage

1. In order to prevent the transfer of the function parameters from being modified, use the const keyword in the form of the call function. / * --------------------- EXAMPLE - ------------------------- * /

INT FINDNUM (Const Int Array [], int Num, int CONUT; // declaration function

// Code ...

INT FINDNUM (Const Int Array [], Int Num, INT Count) {INT I; INT FLAG = 1;

For (i = 0; (i

2. Const can be used to create array constants, pointer constants, pointers pointing to constants, etc .: Const CHAR CH = 'A'; const Int A [5] = {1, 2, 3, 4, 5}; const *p = a; // a is the first address of an array .P is a pointer INT * const p = a; // a is the first address of an array .P is a pointer constant; const * const p = a; / / a is the first address of an array. P is to point out the first two cases of the constant constant constant, that is, convert the rear variable into constant so that the variable cannot be modified. Now we focus on the three uses, because these three cases are easy to make mistakes, even sometimes I am afraid to use the wrong and just crisp.

/ * ---------------------------------------- ---- * /

INT A [] = {1, 2, 3, 4, 5};

--Const int * p = a; // P is a pointer to constant, constly modified INT * p. Therefore, the data in the array cannot be changed by assigning values ​​to the pointer, but the pointer can be moved. For example: // * P = 10; / * Error * / // * (p 2) = 1; / * Error * / / / can be understood: p To point to A, so it does not change any one of them // element Value. But A is not the same, it is free, it wants how to change the value // change.

--Int * const p = a; // Look at this expression, the location of Const and the first difference! Their usage and role are completely different. At this time, P is a constant, we know, the pointer is the first address of an array, then its location cannot be changed. Therefore, the pointer P cannot move. However, it points to the first data of the array, so it can and can only change the value of the first data of the array. Please don't misunderstand this, the pointer constant is just that its address can not change, not the content it points to it must not change! // Recent examples: // * p = 2; / * can * / // * (p 1) = 10; / * can * / // p ; / * No * /

--Const int * const p = a; // If you understand the essentials of the two expressions, this expression you have no problem, CONST has two, and a constant location is the first The location of the situation, the second const is the second case, so this expression of the expression is the first two cases. Not much here! // Next case: // * p = 2; / * No * / // * (p 2) = 10; / * No * / // p ; / * No * / b, Const does not block the modification of parameters

The reason why this is a little, because there are some friends who may not change the parameters in the function parameters, which is actually a wrong understanding, because it does not block the modification of the parameters, below For a simple example to explain;

#include #include void channel (const char * string); int main (void) {char str [] = "the c programme";

ChangeStr (STR); Printf (STR); System ("pause"); return 0;}

Void ChangeStr (const char * string) {char * source = (const char *) String; while (* source) {* source = Toupper (* source); source ;}}

// end

The above program converts each character in the string into uppercase letters. Because * string gives the address * source, and the value of the * source does not interfere, and some compilers may issue a warning. The above program is just to illustrate that constant does not block the modification of the parameters. If you feel like the above program, the individual feels nothing, it will only make people feel easy to confuse.

C. About other monopoly issues below, this keyword is some of the more miscellaneous issues, perhaps the content of the above B can be attributed to this, but because some of the expressions may be a problem, but the compiler is not interference Such a problem, so do not return B to this section. 1. Let's take a look at an example:

/ * ------------------------------- * /

#include

INT main (void) {const Int a = 2; const INT b = 0; const INT * PT = & a; a printf ("% d / n", * pt); // 2 scanf ("% d", PT ); // pt = 4; Printf ("% d / n", * pt); // pt = 4; Pt = & b; // pt = 0; scanf ("% d", pt); // pt = 6; Printf ("% D% d / n", * pt, b); // pt = 6, b = 6 system ("pause"); return 0;} / * -------- ------ The result is like this under VC and DEV-CPP --------- * /

/ / Maybe you can see the problem in this code .//scanf ("% d", pt); ==> I think it is a problem, but there is no discovery in the compiler. Because it directly gives the pointer, although the // may not issue an error message, this is not allowed. The above code is not the case under the LCC compiler. Under the LCC, the value of B does not // The change of the pointer changes. In fact, no matter whether it is legal for the pointer, my opinion is that b cannot be changed as the pointer value pointing to it. This is a small episode. I think this code is not What value and meaning.

2. First look at the code: // code char * p = "abcDef"; // Under the modern compiler, in front of the CHAR actually omitted const; so the value of P can not change, // One change the compiler immediately reports an error ! But in TC, such definitions, the value of P can change because it is / / there is no const to modify, so the P is converted into constants, plus const.

The usage and understanding of Const can only say so much. Of course, there are more advanced or less usage, because the level and experience are limited, it is really unable to say anything. How do you feel that you write to the end, you will write more and more messy!

Third, refer to the reference - "C Primer Plus 5th"

2006.5.21

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

New Post(0)