Access Modifiers
There are two modifiers that control how variables may be accessed or modified. These
qualifiers are const and volatile. They must precede the type modifiers and the type
names that they qualify. These modifiers are also referred to as cv-qualifiers.
There are two modifiers that control how variables may be accessed or modified. These
qualifiers are const and volatile. They must precede the type modifiers and the type
names that they qualify. These modifiers are also referred to as 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;
}
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.
{
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.
No comments:
Post a Comment