Interview questions

J

james

Hi there,

Can anyone explain what's the difference between:

const * char
* const char
const * const char

thanks,
james
 
V

Victor Bazarov

james said:
Can anyone explain what's the difference between:

const * char
* const char
const * const char

All are syntax errors. The difference is in typing.

Real C++ declarations could be

char const * p1; // a pointer to a constant char
const char * p2; // the same as above
char * const p3; // a constant pointer to a char
char const * const p4; // a constant pointer to a constant char.

V
 
T

Thomas Matthews

james said:
Hi there,

Can anyone explain what's the difference between:

const * char
* const char
const * const char

thanks,
james

This is discussed in detail in the C++ FAQ:
http://www.parashift.com/c++-faq-lite/const-correctness.html
One should always consult the FAQ before posting.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

jeffc

Victor Bazarov said:
All are syntax errors. The difference is in typing.

Real C++ declarations could be

char const * p1; // a pointer to a constant char
const char * p2; // the same as above
char * const p3; // a constant pointer to a char
char const * const p4; // a constant pointer to a constant char.

On my compiler, p3 and p4 are part of illegal definitions because they don't
provide initialization.
 
J

jeffc

jeffc said:
On my compiler, p3 and p4 are part of illegal definitions because they don't
provide initialization.

Which leads me to a question (never have been totally sure about this sort
of thing.) If you write
char const * const p4 = 0;
what have you initialized exactly, and if a compiler requires initialization
of const items, why is only 1 initialization required here?
 
J

Jeff Schwab

jeffc said:
Which leads me to a question (never have been totally sure about this sort
of thing.) If you write
char const * const p4 = 0;
what have you initialized exactly, and if a compiler requires initialization
of const items, why is only 1 initialization required here?

You've initialized a variable of type "pointer to constant char." No
char has been defined.
 
J

Jeff Schwab

jeffc said:
I see your point taht no char has been defined, but you mean "constant
pointer to constant char", right?

Yes, you're exactly right. Sorry for the omission!
 
J

jeffc

Jeff Schwab said:
You've initialized a variable of type "pointer to constant char." No
char has been defined.

I see your point taht no char has been defined, but you mean "constant
pointer to constant char", right?
 
V

Victor Bazarov

jeffc said:
On my compiler, p3 and p4 are part of illegal definitions because they don't
provide initialization.

If the declarations are part of a class definition, there can be no
initialiser.

V
 

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