Programming in C (4th Edition) (Developer's Library) by Stephen G. Kochan

Programming in C (4th Edition) (Developer's Library) by Stephen G. Kochan

Author:Stephen G. Kochan [Kochan, Stephen G.]
Language: eng
Format: epub
Publisher: Pearson Education
Published: 2014-08-17T20:00:00+00:00


The Keyword const and Pointers

You have seen how a variable or an array can be declared as const to alert the compiler as well as the reader that the contents of a variable or an array will not be changed by the program. With pointers, there are two things to consider: whether the pointer will be changed, and whether the value that the pointer points to will be changed. Think about that for a second. Assume the following declarations:

char c = 'X';

char *charPtr = &c;

The pointer variable charPtr is set pointing to the variable c. If the pointer variable is always set pointing to c, it can be declared as a const pointer as follows:

char * const charPtr = &c;

(Read this as “charPtr is a constant pointer to a character.”) So, a statement like this:

Click here to view code image

charPtr = &d; // not valid

causes the GNU C compiler to give a message like this:2

2. Your compiler may give a different warning message, or no message at all.

Click here to view code image

foo.c:10: warning: assignment of read-only variable 'charPtr'

Now if, instead, the location pointed to by charPtr will not change through the pointer variable charPtr, that can be noted with a declaration as follows:

const char *charPtr = &c;

(Read this as “charPtr points to a constant character.”) Now of course, that doesn’t mean that the value cannot be changed by the variable c, which is what charPtr is set pointing to. It means, however, that it won’t be changed with a subsequent statement like this:

Click here to view code image

*charPtr = 'Y'; // not valid

which causes the GNU C compiler to issue a message like this:

Click here to view code image

foo.c:11: warning: assignment of read-only location

In the case in which both the pointer variable and the location it points to will not be changed through the pointer, the following declaration can be used:

Click here to view code image

const char * const *charPtr = &c;

The first use of const says the contents of the location the pointer references will not be changed. The second use says that the pointer itself will not be changed. Admittedly, this looks a little confusing, but it’s worth noting at this point in the text.3

3. The keyword const is not used in every program example where it can be employed; only in selected examples. Until you are familiar with reading expressions such as previously shown, it can make understanding the examples more difficult.



Download




Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Web Analytics