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
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.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(11897)
Hello! Python by Anthony Briggs(9373)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9343)
The Mikado Method by Ola Ellnestam Daniel Brolund(9307)
Dependency Injection in .NET by Mark Seemann(8859)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7852)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7324)
Grails in Action by Glen Smith Peter Ledbrook(7296)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7117)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5957)
Kotlin in Action by Dmitry Jemerov(4638)
Practical Vim (for Kathryn Amaral) by Drew Neil(3728)
Cracking the GRE Premium Edition with 6 Practice Tests, 2015 (Graduate School Test Preparation) by Princeton Review(3596)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3417)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3243)
Learning Java by Patrick Niemeyer & Daniel Leuck(2872)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2839)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(2691)
Mastering Java 9 by Dr. Edward Lavieri(2573)