468,456 Members | 1,569 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

const confusion

I have been trying to educate myself with all these different uses of const,
i.e.

char *p
const char *p
char * const p
const char * const p
However, there are a few web pages out there that use some really complex
examples:http://c-faq.com/decl/spiral.anderson.htmlI guess these examples
could be all relevant, however, what's the purpose of such use of so many
const and is there an easier way to solve whatever solution? IMPORTANTALSO,
is there a performance penalty when using all these const? Or isit an
increase? I am thinking an increase, but because it looks so convolutedit
could be slow?Lastly, those function pointers, with const pointerscan be
really hard to read. I know I would use a different and more clearer naming
convention than those in the examples.
Jul 11 '06 #1
2 1481
smnoff posted:
I have been trying to educate myself with all these different uses of
const, i.e.

char *p

Read from right to left.
p is a pointer to a char.

const char *p

p is a pointer to a char which is const.

(Note that it can also be written as: char const *p, in which case you'd
read it as:

p is a pointer to a const char.

char * const p

p is a const pointer to a char.

const char * const p

p is a const pointer to char which is const.

(Note that it can also be written as: char const * const p, in which case
you'd read it as:

p is a const pointer to a const char.
--

Frederick Gotham
Jul 11 '06 #2


smnoff wrote On 07/11/06 11:38,:
I have been trying to educate myself with all these different uses of const,
i.e.

char *p
const char *p
char * const p
const char * const p
However, there are a few web pages out there that use some really complex
examples:http://c-faq.com/decl/spiral.anderson.htmlI guess these examples
could be all relevant, however, what's the purpose of such use of so many
const and is there an easier way to solve whatever solution?
The transition from `int x = 42;' to `const int x = 42;'
is (I hope) easy to understand: The second is just like the
first, with the additional information that x is not to
be changed after it's initialized. x is "read-only."

But C's declarations can be more complex. If we start
with `char *p = "Hello";' there are at least two different
things we might want to make read-only: we might want to
say that p itself is read-only (it will always point to
"Hello"), or that the thing p points to is read-only (so
p cannot be used to replace H with J). Think about this
a bit, and you'll see that "p is read-only" and "*p is
read-only" are different, and are independent (we might
even want to say "both p and *p are read-only").

That's the purpose of having different ways to insert
`const' in a declaration: to allow you to express different
things about what things are and aren't read-only. As for
an "easier way to solve whatever solution," I'm afraid I
can't make sense of the question.
IMPORTANTALSO,
is there a performance penalty when using all these const? Or isit an
increase? I am thinking an increase, but because it looks so convolutedit
could be slow?
Why do you think this is IMPORTANT? Have you made any
measurements of the time penalty for using or omitting `const'?
If you have not, what evidence is there to call it IMPORTANT
rather than TRIVIAL or even NONEXISTENT?

"Look after the pennies, and the pounds will take care
of themselves." Good advice in many circumstances, but not
a universal commandment to fret about minutiae. Or as a friend
of mine once put it: It's always good to get the bottle caps
off the beach so the sand will be nice and clean around the
rotting whale carcase.
Lastly, those function pointers, with const pointerscan be
really hard to read. I know I would use a different and more clearer naming
convention than those in the examples.
With experience you will find them easier to read. There
are only a few common "useful" patterns, and you will learn
to recognize them after a while. Richard Heathfield's post
in the "qsort() not sorting -:(" thread is about the most
complicated const-ing you're likely to run into, or want to
write.

--
Er*********@sun.com

Jul 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by mandydhaliwal | last post: by
7 posts views Thread by Andrej Prsa | last post: by
9 posts views Thread by vashwath | last post: by
7 posts views Thread by siddhu | last post: by
9 posts views Thread by Peithon | last post: by
3 posts views Thread by rwf_20 | last post: by
10 posts views Thread by Stephen Howe | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.