const Keyword Usage Summary

Posted Mon, 17 Oct 2005

Just as a follow up to my last post, here's a quick overview of the const keyword

1. const int x = 5;
2. const int * px;
3. int const * px;
4. const int * const px;

  1. A constant integer with a value of 5. The value of x will be 5 for its entire life.
  2. A pointer to a constant integer. The value of the integer pointed to by px can not be changed; however you may point px to a different integer.
  3. This is the same as #2. The const keyword may appear before or after the type it is qualified on. I prefer the style in #2, but both are syntactically correct.
  4. A constant pointer to a constant integer. You can not change the value of the integer being pointed to, nor can you point px to a different integer.

Related Books

Cocoa(R) Programming for Mac(R) OS X (3rd Edition) C++ Common Knowledge: Essential Intermediate Programming More Effective C++: 35 New Ways to Improve Your Programs and Designs (Addison-Wesley Professional Computing Series) C Programming Language (2nd Edition) (Prentice Hall Software) Programming Interviews Exposed: Secrets to Landing Your Next Job (Programmer to Programmer)

Post a Comment




About

My name is Tim Fanelli, I am a software engineer in Northern NY. I spend most of my time working, and when I can, I try to post interesting things here.

Cigar Dossiers