const confusion

S

smnoff

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.
 
F

Frederick Gotham

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.
 
E

Eric Sosman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top