how to read this function with 2 constants

J

john smith

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

Alf P. Steinbach

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

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

Forum statistics

Threads
473,793
Messages
2,569,640
Members
45,353
Latest member
RogerDoger

Latest Threads

Top