Programming Pointers
In search of contrary evidence
Dan Saks
12/15/2008 12:00 AM EST
I learned to program in C nearly 30 years ago. When support for the const qualifier appeared in compilers some 10 years later, I learned to place it as most programmers do: to the immediate left of the type that it modifies. That is, when I wanted to declare p as a pointer to a constant character, I wrote the declaration as:
const char *p;
as just about everyone else did.
As I grew to understand the const qualifier better, I started to encourage other programmers to use it, which I did by using const whenever appropriate in my writing and in my lectures, and I was not alone. Other writers and speakers were using const as well. Still, many programmers seemed less than enthusiastic about using const.
I started asking programmers why they were reluctant to use const. Most said they just didn't understand it. They knew they could place const at different points within a declaration, and they just didn't understand the consequences of the different placement options. For example, the const in:
const char *p;
modifies the char to its immediate right (it declares p as a pointer to a const char). Does that mean that the const in:
char const* p;
modifies the pointer operator to its immediate right (it declares p as a const pointer to a char)? (Answer: No.)
In the mid-1990s, it occurred to me that it might be easier for programmers to understand const if they learned to apply this fairly simple rule: when writing a declaration, place const to the immediate right of the part of the type that you want to be const; when reading that declaration, read const just before you read the thing to its immediate left. For example, to declare p as a pointer to const char, write:
char const *p;
To read it aloud, just read it from right to left, as "p is a pointer to a const char." To declare p as a const pointer to char, write:
char *const p;
To read it aloud, again just read it from right to left.
I've written earlier columns on why this style makes it easier to read and write declarations using const. 1, 2, 3 You can find them at www.dansaks.com.
In the years that I've been using and teaching this style for placing const in declarations, I received a lot of positive feedback that using this style does indeed help. Unfortunately, the converts represent what appears to be a distinct minority of C and C++ programmers. Most C and C++ programmers simply won't do it. Moreover, they won't even try it.
When I ask programmers why they won't try it, they often tell me, "It's not conventional" or, "Hardly anybody does it that way," to which I offer the following response.
For the last six to eight years, I've been posing the following query to my lecture audiences. Given these declarations in the conventional style:
typedef char *ntcs;
const ntcs s;
what is it about s that's const? Is it (a) the character, or (b) the pointer, or (c) both? (The answer appears at the end of this article.) I've posed this question to thousands (the low single-digit thousands) of programmers and only about 40% get it right.4 The rest either get it wrong or are unable or unwilling to hazard a guess. At the same time, well over 90% of these programmers reported using the conventional style for placing const.
So, when someone tells me they won't use it because it's not conventional, my response is that, in this case, conventional isn't working. Maybe it's time to try something else.





mwelo
12/24/2008 2:50 PM EST
So, const char *p;
is the same as char const *p;
??
Your verbal descriptions are the same...
"p is a pointer to a const char."
Sign in to Reply
Dan Saks
12/26/2008 12:19 AM EST
Yes, they have the same meaning. For an explanation, please read any or all of the articles listed at the end of this article.
Sign in to Reply
David Brown
12/26/2008 9:35 AM EST
Good programming style is about readability. "char const" might sound natural to a Frenchman, but English speakers find "const char" to be a more natural and readable phrase. I can see absolutely no benefit from writing your type phrases backwards - it's inconsistent and breaks the flow of the text. It is not unlike silly rules such as writing "if (1 == x)" instead of "if (x == 1)", where logical writing style is sacrificed for mythical error-checking benefits.
C has a powerful "typedef" statement - use it to make your types and declarations clear. A very simple rule is the type part of a declaration should not have more than two parts - there is no need to worry about the differences between "const char *p", "char const *p" and "char * const p" because the declaration is never written. Instead, use:
typedef char *pchar;
const pchar s;
or
typedef const char cchar;
cchar *p;
(Note the use of more logical names, rather than the non-obvious "typedef char *ntcs".)
Encouraging the correct use of "const" (and "volatile") is a good thing - but please do it by emphasising and encouraging good, clear, *readable* code and good type usage, rather than by inventing your own conventions.
Sign in to Reply
Patrick Perdu
9/13/2010 9:49 PM EDT
I agree with the comment that using typedefs liberally solves the problem whatever the "const" placement style.
Furthermore I find that the article question concerning
typedef char *ntcs;
const ntcs s;
is not a good example because if ntcs is a "pointer to something", then
const ntcs is a const "pointer to something".
On the other hand I do agree that placing the const to the right of the constant part of the type makes it simple.
(And I do not say that because I am a Frenchman - full disclosure: I am)
To me the "const someType" notation is a poor attempt to order the words as an English sentence, even though the rest of the declaration is already written right to left:
type * var
is read in English
"var is a pointer on type"
(which, for the records, is the same order in French :) )
I tend to agree with the comment about
if(VALUE == variable)
but then
(1) it takes little time to get used to it,
(2) you can restrict this coding standard to critical code and
(3) in my experience it did find at least two issues, which means it paid for the effort already.
All the best and happy coding!
Sign in to Reply
vani.k
1/12/2009 3:09 AM EST
hi,
i vandanakacha, i just complete my graduation in computer science from Pune University, India.
i want suggestion from you, i want to do embedded course related to my field i.e. programming. And also tell me which book i want to refer for that. actually i dont know that much about embedded course, but i am interested in this subject. so plz help me & suggest some course related to embedded programing.
Thank you.
Sign in to Reply