Typecasting a pointer to NULL

Discussion in 'C Programming' started by prix prad, Apr 10, 2009.

  1. prix prad

    prix prad Guest

    Hi,
    I have the foll. program. Shoudn't the program crash when the
    NULL pointer is typecast to structure and we try to get the address
    of one of its members.

    I mean how is '&p->c' supposed to work when p is NULL?

    <snip>
    #include <stdio.h>
    typedef struct temp
    {
    int f;
    int k;
    } TEMP;
    typedef struct abc_
    {
    int a;
    TEMP b;
    int c;
    } abc;

    main() {
    void *q = NULL;
    abc *p = NULL;
    int i = (int) (&((abc *)NULL)->c);
    int j = (int) &((abc *)0)->c;
    int k = (int) &((abc *)q)->c;
    int l = (int) &p->c;
    printf("\n i is %d, j is %d, k is %d, l is %d, m is %d", i, j, k,
    l, &p);
    printf("\n Hi: Size of abc is %d\n ", sizeof(abc));
    return (1);
    }
    </snip>

    The o/p is
    i is 12, j is 12, k is 12, l is 12, m is -4195224
    Hi: Size of abc is 16
     
    prix prad, Apr 10, 2009
    #1
    1. Advertising

  2. prix prad

    Flash Gordon Guest

    prix prad wrote:
    > Hi,
    > I have the foll. program. Shoudn't the program crash when the
    > NULL pointer is typecast to structure and we try to get the address
    > of one of its members.


    What guarantee is there that if you walk in the fast lane of a motorway
    you will get hit by a car? Undefined behaviour is like that, sometimes
    it causes a major incident, other times nothing untoward seems to happen.

    Yes, I *have* walked in the fast lane of a motorway, but that was
    because the motorway was blocked so no vehicles were moving.

    > I mean how is '&p->c' supposed to work when p is NULL?


    <snip code>

    It isn't supposed to work, nor is it supposed to crash. It is undefined
    behaviour and anything can happen. Note though that in this case after
    the dereference you then take the address, so one possible result is the
    two operators cancelling out.

    I'm guessing you have been looking at either the definition of the
    standard offsetof macro, or someones attempt to write its equivalent.
    Look up the offsetof macro in your C reference and use that (or tell the
    author of the code to use it) when that is what you want!
    --
    Flash Gordon
     
    Flash Gordon, Apr 10, 2009
    #2
    1. Advertising

  3. prix prad

    CBFalconer Guest

    prix prad wrote:
    >
    > I have the foll. program. Shoudn't the program crash when the
    > NULL pointer is typecast to structure and we try to get the
    > address of one of its members.


    No. Casting NULL to another pointer type results in NULL.

    >
    > I mean how is '&p->c' supposed to work when p is NULL?


    It isn't. It didn't. It won't.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Apr 11, 2009
    #3
  4. prix prad

    James Kuyper Guest

    CBFalconer wrote:
    > prix prad wrote:
    >> I have the foll. program. Shoudn't the program crash when the
    >> NULL pointer is typecast to structure and we try to get the
    >> address of one of its members.

    >
    > No. Casting NULL to another pointer type results in NULL.


    No, it results in a null pointer of the specified type. That null
    pointer must compare equal to NULL, but it is not NULL. NULL is the name
    of a standard macro; null is an adjective that can be applied to (among
    other things) pointer values. Please keep them distinct.
     
    James Kuyper, Apr 11, 2009
    #4
  5. On Sat, 11 Apr 2009 21:15:19 +0200, Richard <> wrote:

    >CBFalconer <> writes:
    >
    >> prix prad wrote:
    >>>
    >>> I have the foll. program. Shoudn't the program crash when the
    >>> NULL pointer is typecast to structure and we try to get the
    >>> address of one of its members.

    >>
    >> No. Casting NULL to another pointer type results in NULL.
    >>
    >>>
    >>> I mean how is '&p->c' supposed to work when p is NULL?

    >>
    >> It isn't. It didn't. It won't.

    >
    >That's a surprise since
    >
    >sizeof(&p->c) works just fine....


    Chuck may be a pain but claiming that success using a non-evaluated
    expression implies success when the expression is evaluated is just
    technically inaccurate. The code in the OP attempts to dereference a
    null pointer and Chuck is correct that it doesn't work, regardless of
    the results.

    --
    Remove del for email
     
    Barry Schwarz, Apr 12, 2009
    #5
  6. prix prad

    CBFalconer Guest

    James Kuyper wrote:
    > CBFalconer wrote:
    >> prix prad wrote:
    >>
    >>> I have the foll. program. Shoudn't the program crash when the
    >>> NULL pointer is typecast to structure and we try to get the
    >>> address of one of its members.

    >>
    >> No. Casting NULL to another pointer type results in NULL.

    >
    > No, it results in a null pointer of the specified type. That
    > null pointer must compare equal to NULL, but it is not NULL.
    > NULL is the name of a standard macro; null is an adjective
    > that can be applied to (among other things) pointer values.
    > Please keep them distinct.


    Correction accepted. I do tend to leave out the (unimportant to
    me) details.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Apr 12, 2009
    #6
    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. Replies:
    5
    Views:
    27,539
    Mike Schilling
    Mar 29, 2006
  2. venkatesh
    Replies:
    1
    Views:
    8,280
    lallous
    Dec 6, 2003
  3. Robert Street

    Advanced pointer typecasting

    Robert Street, Feb 20, 2004, in forum: C++
    Replies:
    3
    Views:
    6,997
    Robert Street
    Feb 21, 2004
  4. aneuryzma
    Replies:
    3
    Views:
    781
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    466
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page