Basic interview question on pointers

Discussion in 'C++' started by pauldepstein@att.net, Mar 2, 2009.

  1. Guest

    From a website:

    25. Is there any problem with the following:
    char *a=NULL; char& p = *a;?
    The result is undefined. You should never do this. A reference must
    always refer to some object.



    I'm a bit puzzled. I would have said there are two problems. One is
    mentioned on the website. However, isn't there another problem --
    that it's wrong to dereference a null pointer?


    Thanks,

    Paul Epstein
    , Mar 2, 2009
    #1
    1. Advertising

  2. Stefan Ram Guest

    writes:
    >25. Is there any problem with the following:
    >char *a=NULL; char& p = *a;?
    >However, isn't there another problem --
    >that it's wrong to dereference a null pointer?


    One can not know whether »NULL« is a null pointer.
    The following should be a legal C++ translation unit:

    #define NULL "NULL"
    int main(){ char *a=NULL; char& p = *a; }
    Stefan Ram, Mar 3, 2009
    #2
    1. Advertising

  3. Triple-DES Guest

    On 2 Mar, 21:57, wrote:
    > From a website:
    >
    > 25.    Is there any problem with the following:
    > char *a=NULL; char& p = *a;?
    > The result is undefined. You should never do this. A reference must
    > always refer to some object.
    >
    > I'm a bit puzzled.  I would have said there are two problems.  One is
    > mentioned on the website.  However, isn't there another problem --
    > that it's wrong to dereference a null pointer?
    >
    > Thanks,
    >
    > Paul Epstein


    If that website is the one I think it is, be very careful. Some of the
    answers are fairly accurate, others are inaccurate (like this), and
    some are just plain wrong.

    See Q18 for an example of the latter.
    Question: Can a constructor throw a exception? How to handle the error
    when the constructor fails?
    Answer: The constructor never throws a error.

    This is blatantly false.
    Triple-DES, Mar 3, 2009
    #3
  4. James Kanze Guest

    On Mar 3, 1:10 am, -berlin.de (Stefan Ram) wrote:
    > writes:
    > >25. Is there any problem with the following:
    > >char *a=NULL; char& p = *a;?
    > >However, isn't there another problem --
    > >that it's wrong to dereference a null pointer?


    > One can not know whether »NULL« is a null pointer.
    > The following should be a legal C++ translation unit:


    > #define NULL "NULL"
    > int main(){ char *a=NULL; char& p = *a; }


    But only as long as no standard headers are included.
    Realistically, you should be able to assume that NULL is the one
    defined in <stddef.h>.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 3, 2009
    #4
  5. Stefan Ram Guest

    James Kanze <> writes:
    >>#define NULL "NULL"
    >>int main(){ char *a=NULL; char& p = *a; }

    >But only as long as no standard headers are included.


    I did not see any include directives in the OP.

    >Realistically, you should be able to assume that NULL is the one
    >defined in <stddef.h>.


    In the »real world« (when given code to review or to
    refactor) I have a complete compilation unit
    and can see which include directives are used.

    This shows that not only in Usenet questions but
    also in interviews, it is better to use an SSCCE.

    http://sscce.org/
    Stefan Ram, Mar 3, 2009
    #5
  6. Kai-Uwe Bux Guest

    Stefan Ram wrote:

    > James Kanze <> writes:
    >>>#define NULL "NULL"
    >>>int main(){ char *a=NULL; char& p = *a; }

    >>But only as long as no standard headers are included.

    >
    > I did not see any include directives in the OP.
    >
    >>Realistically, you should be able to assume that NULL is the one
    >>defined in <stddef.h>.

    >
    > In the »real world« (when given code to review or to
    > refactor) I have a complete compilation unit
    > and can see which include directives are used.
    >
    > This shows that not only in Usenet questions but
    > also in interviews, it is better to use an SSCCE.
    >
    > http://sscce.org/


    Actually, it doesn't show that an SSCCE is better for interview questions.
    Using a snippet may invite some smart aleck to answer. This, you want to
    avoid on usenet; but in an interview situation, such an answer can provide
    useful information to the interviewer.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Mar 3, 2009
    #6
  7. James Kanze Guest

    On Mar 3, 3:24 pm, -berlin.de (Stefan Ram) wrote:
    > James Kanze <> writes:
    > >>#define NULL "NULL"
    > >>int main(){ char *a=NULL; char& p = *a; }

    > >But only as long as no standard headers are included.


    > I did not see any include directives in the OP.


    > >Realistically, you should be able to assume that NULL is the one
    > >defined in <stddef.h>.


    > In the »real world« (when given code to review or to
    > refactor) I have a complete compilation unit
    > and can see which include directives are used.


    Only directly. If you include <vector>, you don't know whether
    NULL is defined or not.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 4, 2009
    #7
    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. Simon

    Basic pointers question

    Simon, Nov 17, 2006, in forum: C Programming
    Replies:
    6
    Views:
    318
  2. Jeff Bender
    Replies:
    6
    Views:
    327
    Jeff Bender
    Jun 8, 2007
  3. Mindsports
    Replies:
    0
    Views:
    451
    Mindsports
    Aug 28, 2009
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    655
  5. reema
    Replies:
    0
    Views:
    242
    reema
    Aug 26, 2008
Loading...

Share This Page