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

const qualifier usage

P: n/a
const int *pci ; //pci points to const int
int *const cpi ; //cpi is a const pointer to int

int const *dunno ; //I don't know what this declaration means??

Could somebody help.

thanks
Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a


Sorav Bansal wrote:
const int *pci ; //pci points to const int
int *const cpi ; //cpi is a const pointer to int

int const *dunno ; //I don't know what this declaration means??


Same as the first one: `dunno' is a pointer to an
unmodifiable `int'. (Unmodifiable by means of `dunno',
at any rate -- the target `int' may be modifiable by
other means.)

--
Er*********@sun.com

Nov 15 '05 #2

P: n/a
Eric Sosman wrote:

Sorav Bansal wrote:
const int *pci ; //pci points to const int
int *const cpi ; //cpi is a const pointer to int

int const *dunno ; //I don't know what this declaration means??

Same as the first one: `dunno' is a pointer to an
unmodifiable `int'. (Unmodifiable by means of `dunno',
at any rate -- the target `int' may be modifiable by
other means.)


thanks!
Nov 15 '05 #3

P: n/a
On Wed, 13 Jul 2005 11:51:02 -0700, Sorav Bansal
<sb*****@stanford.edu> wrote:
const int *pci ; //pci points to const int
int *const cpi ; //cpi is a const pointer to int

int const *dunno ; //I don't know what this declaration means??


It means the same as the first form, a pointer to constant int. Why it
is an allowed form I don't know, perhaps someone was having a bad day
and wanted to make things confusing...

$ cdecl
int const *dunno ;
declare dunno as pointer to int const;

Chris C
Nov 15 '05 #4

P: n/a
Sorav Bansal wrote:
const int* pci; // int const* pci;
const int ci = 0; // int const ci = 0;
pci = &ci; // ok!
*pci = 42; // error!

int i; int* const cpi = &i;
*cpi = 0; // ok!
cpi = NULL; // error!
int const* dunno; // const int* dunno;


dunno = &ci; // ok!
*dunno = 42; // error!

Nov 15 '05 #5

P: n/a
Chris Croughton wrote:
On Wed, 13 Jul 2005 11:51:02 -0700, Sorav Bansal
<sb*****@stanford.edu> wrote:
const int *pci ; //pci points to const int
int *const cpi ; //cpi is a const pointer to int

int const *dunno ; //I don't know what this declaration means??


It means the same as the first form, a pointer to constant int. Why it
is an allowed form I don't know, perhaps someone was having a bad day
and wanted to make things confusing...


Do you mean, why is "const int *pci" an allowed form?
In all cases except for that one, 'const' comes immediately
to the right of the type that it's modifying.

Nov 15 '05 #6

P: n/a
On Wed, 13 Jul 2005 14:38:03 -0700, Old Wolf wrote:
Chris Croughton wrote:
On Wed, 13 Jul 2005 11:51:02 -0700, Sorav Bansal
<sb*****@stanford.edu> wrote:
> const int *pci ; //pci points to const int
> int *const cpi ; //cpi is a const pointer to int
>
> int const *dunno ; //I don't know what this declaration means??
It means the same as the first form, a pointer to constant int. Why it
is an allowed form I don't know, perhaps someone was having a bad day
and wanted to make things confusing...


Do you mean, why is "const int *pci" an allowed form?


Well it is the form a lot of programmers prefer.
In all cases except for that one, 'const' comes immediately
to the right of the type that it's modifying.


What you refer to as "all cases" is only a single case: const may follow a
* (pointer) declarator. The language makes an important distinction
between declaration specifiers (static, char, unsigned, const, struct
etc.) and declarators (pointer, array and function derivations). It makes
sense to be able to distinguish between the two when reading a
declaration, the issue here being that const (and volatile) can appear in
both. Of course there is potential for real nastiness such as

int static short volatile unsigned

Lawrence

Nov 15 '05 #7

P: n/a
On 13 Jul 2005 14:38:03 -0700, Old Wolf
<ol*****@inspire.net.nz> wrote:
Chris Croughton wrote:
On Wed, 13 Jul 2005 11:51:02 -0700, Sorav Bansal
<sb*****@stanford.edu> wrote:
> const int *pci ; //pci points to const int
> int *const cpi ; //cpi is a const pointer to int
>
> int const *dunno ; //I don't know what this declaration means??
It means the same as the first form, a pointer to constant int. Why it
is an allowed form I don't know, perhaps someone was having a bad day
and wanted to make things confusing...


Do you mean, why is "const int *pci" an allowed form?


No, I mean why is "int const" allowed.
In all cases except for that one, 'const' comes immediately
to the right of the type that it's modifying.


Which is itself confusing.

Chris C
Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.