CONST use in C language

xiaoxiao2021-03-06  45

Const and pass-by-value

Use a constant prefix (const) to avoid modification of the address variable:

Void F (Const string & s);

Some developers also use consts even for pilot value variables:

Void F (const Int n); / * n is passed by value, why const? * /

Is ConST really necessary? No, no need. Remember, when you use the pass value variable, the call function does not modify the variable value and only copy it. Further, according to the C standard, Top-level CV-Qualification prefix is Ignorable. Let us explain this term: "CV-Qualification" refers to the ordinary and unstable. "Top-Level" means that the parameter is not a combination or non-intact type, such as: not a pointer, reference or array. This:

Void F (int const param1);

Still treated as the following manner:

Void f (int param1);

To do this, pass a parameter of the type 'const Int' type to Void F (int param1); it is allowed:

Void f (int N); / * non-const parameter * /

int main ()

{

f (1); / * const Int; Fine * /

}

Instead, the following is an impact on the use of constants:

Void F (int * p1);

Void f (const INT * P2);

Here, constants are used in a combined type, called int *. Thus, the type "a pointer to constant integersion" cannot be transmitted to the first function f ().

Void F (int * p1); / * non const version * /

INT n = 0;

Const Int * P = & n;

f (p); / * error, Cannot Convert 'const INT *' To 'int *' * /

As a rule, if the variable passes or returns a constant as a constant in a pass value, only the constant type is used for the combined type.

-----------

Danny Kalev

http://www3.ccw.com.cn/club/essence/200104/869.htm

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

New Post(0)