const qualifier usage

S

Sorav Bansal

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
 
E

Eric Sosman

Sorav said:
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.)
 
S

Sorav Bansal

Eric said:
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!
 
C

Chris Croughton

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
 
O

Old Wolf

Chris said:
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.
 
L

Lawrence Kirby

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
 
C

Chris Croughton

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
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top