don't understand this deal with const pointers in this trivial example

J

johny smith

Well,

I thought that I was creating a constant pointer, and would not be able to
increment it without a compiler error.

But this example compiles fine.

The const is to the left of the * so I thought this would force that to be
constanst.

What am I missing? thanks

#include <iostream>


void f( int const * a );


int main()
{

int a = 5;

f( &a );



return 0;

}


void f( int const * b )
{

b++;
}
 
J

John Carson

johny smith said:
Well,

I thought that I was creating a constant pointer, and would not be
able to increment it without a compiler error.

But this example compiles fine.

The const is to the left of the * so I thought this would force that
to be constanst.

It is the opposite. const to the left of * means constancy of the thing
pointed to (an int in this case). const to the right of * means the pointer
is constant. You are incrementing the pointer, not the int, which your code
allows.
 
D

David White

johny smith said:
Well,

I thought that I was creating a constant pointer, and would not be able to
increment it without a compiler error.

But this example compiles fine.

The const is to the left of the * so I thought this would force that to be
constanst.

What am I missing? thanks

#include <iostream>


void f( int const * a );

To decipher this declaration, start at the 'a' and follow any modifiers
(const or volatile) and operators, in order of precedence, outwards. First
comes the * operator, so 'a' is a pointer. Next is 'const', so 'a' is a
pointer to a const. Last is 'int', so 'a' is a pointer to a const int. If
you want the pointer to be const and not what it points to, change it to:
int * const a
If you follow the same process on this, you end up with: const pointer to
int.
int main()
{

int a = 5;

f( &a );



return 0;

}


void f( int const * b )
{

b++;
}

DW
 
J

John Harrison

Well,

I thought that I was creating a constant pointer, and would not be able
to
increment it without a compiler error.

But this example compiles fine.

The const is to the left of the * so I thought this would force that to
be
constanst.

What am I missing? thanks

Others have explained your mistake, but maybe your confusion arises from
ambiguous terminolgy. Constant pointers are pretty rare (usually you would
use a reference instead), but pointers to constant data are very common,
therefore people use the term constant pointer to actually mean pointer to
constant data.

john
 
D

David White

John Harrison said:
On Mon, 5 Jul 2004 19:58:38 -0600, johny smith

Others have explained your mistake, but maybe your confusion arises from
ambiguous terminolgy. Constant pointers are pretty rare

Yes, and they are particularly rare as function parameters. There's not much
point making any pass-by-value type const because it doesn't matter if you
change it.
(usually you would
use a reference instead), but pointers to constant data are very common,
therefore people use the term constant pointer to actually mean pointer to
constant data.

DW
 
K

Karl Heinz Buchegger

johny said:
Well,

I thought that I was creating a constant pointer, and would not be able to
increment it without a compiler error.

But this example compiles fine.

The const is to the left of the * so I thought this would force that to be
constanst.

What am I missing?

const applies to the thing on its left. With the only exception
of const beeing the leftmost keyword. Then it applies on the
thing on its right.
 
J

JKop

const int * chocolate;

int const * chocolate;


Both of the above are: const pointer to a non-const int. Note how the const
is NOT touching the variable name, it's not directly beside it.


int* const chocolate;

Here, it's touching the variable name. What we have is: a non-const pointer
to a const object.


And then we have:

const int* const chocolate;
int const* const chocolate;

a const pointer to a const object.


-JKop
 
J

JKop

IGNORE MY LAST POST


const int * chocolate;

int const * chocolate;


Both of the above are: non-const pointer to a const int. Note how the const
is NOT touching the variable name, it's not directly beside it, so the
varible itself is not const.


int* const chocolate;

Here, it's touching the variable name. What we have is: a const pointer
to a non-const object.


And then we have:

const int* const chocolate;
int const* const chocolate;

a const pointer to a const object.


-JKop
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top