Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > don't understand this deal with const pointers in this trivial example

Reply
Thread Tools

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

 
 
johny smith
Guest
Posts: n/a
 
      07-06-2004
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++;
}


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      07-06-2004
"johny smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> 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.

> 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++;
> }



--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      07-06-2004
"johny smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-06-2004
On Mon, 5 Jul 2004 19:58:38 -0600, johny smith
<(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      07-06-2004
"John Harrison" <(E-Mail Removed)> wrote in message
newspsao9dtlz212331@andronicus...
> On Mon, 5 Jul 2004 19:58:38 -0600, johny smith
> <(E-Mail Removed)> wrote:
>
> 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


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-06-2004
johny smith wrote:
>
> 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.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-06-2004

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

 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-06-2004

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to understand "u_long operator() (const KeyType kt) const" thomas C++ 1 02-14-2008 05:51 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
how to deal with the translation from "const char * " to "const unsigned char *"? =?gb2312?B?wNbA1rTzzOzKpg==?= C++ 26 01-30-2007 02:40 AM
trivial or non-trivial object baibaichen C++ 3 01-12-2006 03:01 PM



Advertisments