The const and volatile Qualifiers. McGraw-Hill The Complete Reference Fourth Edition
Advertentie
Advertentie
C h a p t e r 2 : E x p r e s s i o n s
23 and unwanted side effects. A major problem in developing large programs is the accidental changing of a variable's value because it was used elsewhere in the program.
This can happen in C/C++ if you use too many global variables in your programs.
The const and volatile Qualifiers
There are two qualifiers that control how variables may be accessed or modified:
const
and volatile. They must precede the type modifiers and the type names that they qualify. These qualifiers are formally referred to as the cv-qualifiers.
const
Variables of type const may not be changed by your program. (A const variable can be given an initial value, however.) The compiler is free to place variables of this type into read-only memory (ROM). For example, const int a=10; creates an integer variable called a with an initial value of 10 that your program may not modify. However, you can use the variable a in other types of expressions.
A const variable will receive its value either from an explicit initialization or by some hardware-dependent means.
The const qualifier can be used to protect the objects pointed to by the arguments to a function from being modified by that function. That is, when a pointer is passed to a function, that function can modify the actual variable pointed to by the pointer. However, if the pointer is specified as const in the parameter declaration, the function code won't be able to modify what it points to. For example, the sp_to_dash( ) function in the following program prints a dash for each space in its string argument. That is, the string
"this is a test" will be printed as "this-is-a-test". The use of const in the parameter declaration ensures that the code inside the function cannot modify the object pointed to by the parameter.
#include <stdio.h> void sp_to_dash(const char *str); int main(void)
{ sp_to_dash("this is a test");
} return 0;
24
C + + : T h e C o m p l e t e R e f e r e n c e void sp_to_dash(const char *str)
{ while(*str) { if(*str== ' ') printf("%c", '-'); else printf("%c", *str); str++;
}
}
If you had written sp_to_dash( ) in such a way that the string would be modified, it would not compile. For example, if you had coded sp_to_dash( ) as follows, you would receive a compile-time error:
/* This is wrong. */ void sp_to_dash(const char *str)
{ while(*str) { if(*str==' ' ) *str = '-'; /* can't do this; str is const */ printf("%c", *str); str++;
}
}
Many functions in the standard library use const in their parameter declarations.
For example, the strlen( ) function has this prototype: size_t strlen(const char *str);
Specifying str as const ensures that strlen( ) will not modify the string pointed to by str.
In general, when a standard library function has no need to modify an object pointed to by a calling argument, it is declared as const.
You can also use const to verify that your program does not modify a variable.
Remember, a variable of type const can be modified by something outside your program. For example, a hardware device may set its value. However, by declaring a variable as const, you can prove that any changes to that variable occur because of external events.
volatile
The modifier volatile tells the compiler that a variable's value may be changed in ways not explicitly specified by the program. For example, a global variable's address may be passed to the operating system's clock routine and used to hold the real time of the
Downloaden
Advertentie