4 Years
Discussion Span
Last Post by rubberman

The character(s) pointed to by buf are treated as read-only. To make the pointer itself const, place the keyword after the asterisk:

char * const buf;       // const pointer to non-const char
const char *buf;        // Non-const pointer to const char
const char * const buf; // const pointer to const char

Edited by deceptikon


Thank you.
But now I'm confused about the placement of the asterisk.
Does const char* buf mean the same thing as const char *buf or
const char * buf?


For the most part, whitespace in C is ignored. All three of your examples are functionally identical. You could also say const char*buf and it'll work fine because the tokens aren't ambiguous. However, constchar won't work.

Another bit of trivia is that const doesn't have to go before the type: char const *buf. This can make declarations easier to decipher as they're typically read inside out and right to left: char const * const buf reads as "buf is a const pointer to const char".


Good luck Kent! :-) Sometimes this stuff gets my eyeballs rolling, and I've been doing it for 30+ years! Deceptikon's answer was to the point. Couldn't state it better myself.

Edited by rubberman

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.