By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

how to read this function with 2 constants

P: n/a
if I have:

void f( const int* const p )

Do I read this as.

P is a constant pointer to an int whose value is constant?

Or, is there another interpretation?

Thanks alot for any input.
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
* john smith:

NNTP-Posting-Date: Tue, 12 Apr 2005 18:53:19 MST
Date: Mon, 11 Apr 2005 20:03:55 -0700
Xref: uni-berlin.de comp.lang.c++:859528
Seems to be something wrong with your client software or server.

if I have:

void f( const int* const p )

Do I read this as.

P is a constant pointer to an int whose value is constant?
Yes.

Or, is there another interpretation?


No, but there are a few points to be aware of.

First, placing 'const' at the front is a special case that's allowed
for unfathomable reasons (making the syntax even more complex?); in
the general notation you'd write it as

int const * const p

Second, for a function _definition_ it matters that p is 'const', because
that restricts the usage of p inside the function body. But for a pure
function _declaration_ it does not matter whether p is declared 'const'
or not, because the caller must supply a value anyway, and what happens
to that value from the caller's perspective is the same anyway. And in
fact it's much stronger than "doesn't matter": when the compiler checks
whether two declarations are compatible it first removes top-level 'const'.

However, the 'const' for the 'int' (which is not a top-level 'const') matters
also for the declaration, because that affects what the function can do to the
'int' that the caller supplies a pointer to.

If this sounds confusing, forget it, just remember that there is something.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.