Interview questions

Discussion in 'C++' started by james, Feb 18, 2004.

  1. james

    james Guest

    Hi there,

    Can anyone explain what's the difference between:

    const * char
    * const char
    const * const char

    thanks,
    james
    james, Feb 18, 2004
    #1
    1. Advertising

  2. "james" <> wrote...
    > 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
    Victor Bazarov, Feb 18, 2004
    #2
    1. Advertising

  3. james wrote:

    > 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
    Thomas Matthews, Feb 18, 2004
    #3
  4. james

    jeffc Guest

    "Victor Bazarov" <> wrote in message
    news:vCCYb.336776$I06.3521124@attbi_s01...
    > "james" <> wrote...
    > > 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.


    On my compiler, p3 and p4 are part of illegal definitions because they don't
    provide initialization.
    jeffc, Feb 18, 2004
    #4
  5. james

    jeffc Guest

    "jeffc" <> wrote in message
    news:...
    > >
    > > 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.


    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?
    jeffc, Feb 18, 2004
    #5
  6. james

    Jeff Schwab Guest

    jeffc wrote:
    > "jeffc" <> wrote in message
    > news:...
    >
    >>>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.

    >
    >
    > 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.
    Jeff Schwab, Feb 19, 2004
    #6
  7. james

    Jeff Schwab Guest

    jeffc wrote:
    > "Jeff Schwab" <> wrote in message
    > news:...
    >
    >>jeffc wrote:
    >>
    >>>"jeffc" <> wrote in message
    >>>news:...
    >>>
    >>>
    >>>>>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.
    >>>
    >>>
    >>>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.

    >
    >
    > 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!
    Jeff Schwab, Feb 19, 2004
    #7
  8. james

    jeffc Guest

    "Jeff Schwab" <> wrote in message
    news:...
    > jeffc wrote:
    > > "jeffc" <> wrote in message
    > > news:...
    > >
    > >>>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.

    > >
    > >
    > > 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.


    I see your point taht no char has been defined, but you mean "constant
    pointer to constant char", right?
    jeffc, Feb 19, 2004
    #8
  9. "jeffc" <> wrote...
    >
    > "Victor Bazarov" <> wrote in message
    > news:vCCYb.336776$I06.3521124@attbi_s01...
    > > "james" <> wrote...
    > > > 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.

    >
    > 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
    Victor Bazarov, Feb 20, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vishal Pandey

    Interview Questions

    Vishal Pandey, Nov 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    601
    Vishal Pandey
    Nov 24, 2003
  2. Jobs
    Replies:
    0
    Views:
    1,009
  3. Dougie

    ASP.Net Interview Questions

    Dougie, Apr 27, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    964
    Cowboy \(Gregory A. Beamer\)
    Apr 27, 2004
  4. Vijay
    Replies:
    2
    Views:
    566
    Öö Tiib
    May 7, 2010
  5. reema
    Replies:
    0
    Views:
    241
    reema
    Aug 26, 2008
Loading...

Share This Page