strange integer-pointer behaviour

Discussion in 'C Programming' started by matevzb, Aug 13, 2007.

  1. matevzb

    matevzb Guest

    I've ran into some fishy code that, at first glance, is buggy, but it
    seems to work correctly
    and none of the compilers I've tried (five so far, on various systems)
    gives any warnings.
    The code:
    ============================
    #include <stdio.h>

    void
    fcn (char *str)
    {
    if (str == '\0')
    {
    printf ("str!\n");
    }
    }

    int
    main (void)
    {
    fcn ('\0');
    return 0;
    }
    ============================
    My understanding so far: '\0' is passed to fcn(), it equals integer 0,
    which in turn "equals"
    a NULL pointer in terms of zero-ness. The 0 in fcn() is then compared
    to '\0' which is also a 0.
    No warnings, no errors, code works (or so it seems).
    But - if I pass e.g. '\1' to fcn() than all the compilers complain
    with similar warnings,
    i.e. making a pointer from integer without a cast.

    My question is, why don't the compilers complain when '\0' is passed?
    Shouldn't they give
    the same warning, as it's also an integer (albeit 0 in value)?

    --
    WYCIWYG - what you C is what you get
     
    matevzb, Aug 13, 2007
    #1
    1. Advertising

  2. matevzb

    Eric Sosman Guest

    matevzb wrote On 08/13/07 13:35,:
    > I've ran into some fishy code that, at first glance, is buggy, but it
    > seems to work correctly
    > and none of the compilers I've tried (five so far, on various systems)
    > gives any warnings.
    > The code:
    > ============================
    > #include <stdio.h>
    >
    > void
    > fcn (char *str)
    > {
    > if (str == '\0')
    > {
    > printf ("str!\n");
    > }
    > }
    >
    > int
    > main (void)
    > {
    > fcn ('\0');
    > return 0;
    > }
    > ============================
    > My understanding so far: '\0' is passed to fcn(), it equals integer 0,
    > which in turn "equals"
    > a NULL pointer in terms of zero-ness. The 0 in fcn() is then compared
    > to '\0' which is also a 0.
    > No warnings, no errors, code works (or so it seems).
    > But - if I pass e.g. '\1' to fcn() than all the compilers complain
    > with similar warnings,
    > i.e. making a pointer from integer without a cast.
    >
    > My question is, why don't the compilers complain when '\0' is passed?
    > Shouldn't they give
    > the same warning, as it's also an integer (albeit 0 in value)?


    A literal constant zero[*] used in a pointer context
    *is* a null pointer constant. In the code above, '\0'
    is just a decorative way to write a literal constant zero.
    The compiler sees that it's being used where a pointer is
    expected, so the compiler recognizes the zero as a null
    pointer constant instead of as an integer. Indeed, you
    will find that on many systems the NULL macro is defined
    as an unadorned zero.

    [*] Any zero-valued constant integer expression will
    do; try `fcn (42 / 100)', for example. Also, the
    expression is still a null pointer constant if it
    is cast to `void*'.

    This special treatment applies only to

    - Zero-valued expressions: If the expression's value
    is non-zero, the expression cannot be a null pointer
    constant. That's why the compiler complained when
    you tried to use '\1' as an argument to fcn().

    - Integer-valued expressions: If the expression's type
    is `double' or `float' or `struct fubar', it is not
    a null pointer constant. fcn(0.0) won't work.

    - Constant expressions: If the expression is not a
    compile-time constant, it is not a null pointer
    constant. fcn(x - x) won't work; you can deduce
    that the argument will always be zero, but from
    the compiler's point of view it's just a remarkable
    coincidence.

    The FAQ has a section devoted to null pointers; you
    might find it helpful.

    --
     
    Eric Sosman, Aug 13, 2007
    #2
    1. Advertising

  3. matevzb

    matevzb Guest

    On Aug 13, 7:58 pm, Eric Sosman <> wrote:
    > A literal constant zero[*] used in a pointer context
    > *is* a null pointer constant. In the code above, '\0'
    > is just a decorative way to write a literal constant zero.
    > The compiler sees that it's being used where a pointer is
    > expected, so the compiler recognizes the zero as a null
    > pointer constant instead of as an integer. Indeed, you
    > will find that on many systems the NULL macro is defined
    > as an unadorned zero.

    Yes, sorry about even posting this. Moments after I posted, I realized
    that all bets are off with 0 with regards to (pointer) type
    checking...
    Thanks

    --
    WYCIWYG - what you C is what you get
     
    matevzb, Aug 13, 2007
    #3
  4. matevzb

    Eric Sosman Guest

    matevzb wrote On 08/13/07 14:30,:
    > On Aug 13, 7:58 pm, Eric Sosman <> wrote:
    >
    >> A literal constant zero[*] used in a pointer context
    >>*is* a null pointer constant. In the code above, '\0'
    >>is just a decorative way to write a literal constant zero.
    >>The compiler sees that it's being used where a pointer is
    >>expected, so the compiler recognizes the zero as a null
    >>pointer constant instead of as an integer. Indeed, you
    >>will find that on many systems the NULL macro is defined
    >>as an unadorned zero.

    >
    > Yes, sorry about even posting this. Moments after I posted, I realized
    > that all bets are off with 0 with regards to (pointer) type
    > checking...


    Well, no, not exactly. There is no need to type-check
    a NULL, because NULL is a perfectly good value for any kind
    of pointer at all. The confusion (what remains of it) comes
    from the fact that there are many ways to spell NULL.

    Gertrude Stein never said "A NULL is a '\0' is a 0,"
    but she could have.

    --
     
    Eric Sosman, Aug 13, 2007
    #4
  5. matevzb

    CBFalconer Guest

    matevzb wrote:
    >
    > I've ran into some fishy code that, at first glance, is buggy, but
    > it seems to work correctly and none of the compilers I've tried
    > (five so far, on various systems) gives any warnings. The code:
    >
    > #include <stdio.h>
    >
    > void fcn (char *str) {


    This wants a pointer to char as the parameter.

    > if (str == '\0') {


    This tests the parameter against a fixed char.

    > printf ("str!\n");
    > }
    > }
    >
    > int main (void) {
    > fcn ('\0');


    This passes an integer (char representation) to something that
    wants a char. pointer.

    > return 0;
    > }


    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Aug 13, 2007
    #5
  6. Eric Sosman <> writes:
    > matevzb wrote On 08/13/07 14:30,:
    >> On Aug 13, 7:58 pm, Eric Sosman <> wrote:
    >>> A literal constant zero[*] used in a pointer context
    >>>*is* a null pointer constant. In the code above, '\0'
    >>>is just a decorative way to write a literal constant zero.
    >>>The compiler sees that it's being used where a pointer is
    >>>expected, so the compiler recognizes the zero as a null
    >>>pointer constant instead of as an integer. Indeed, you
    >>>will find that on many systems the NULL macro is defined
    >>>as an unadorned zero.

    >>
    >> Yes, sorry about even posting this. Moments after I posted, I realized
    >> that all bets are off with 0 with regards to (pointer) type
    >> checking...

    >
    > Well, no, not exactly. There is no need to type-check
    > a NULL, because NULL is a perfectly good value for any kind
    > of pointer at all. The confusion (what remains of it) comes
    > from the fact that there are many ways to spell NULL.
    >
    > Gertrude Stein never said "A NULL is a '\0' is a 0,"
    > but she could have.


    On the other hand, I'd like my compiler to warn me about using '\0' as
    a null pointer constant, at least optionally. It's perfectly legal,
    but it usually indicates a conceptual error on the part of the
    programmer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 13, 2007
    #6
  7. matevzb

    Default User Guest

    Eric Sosman wrote:

    > matevzb wrote On 08/13/07 14:30,:


    > > Yes, sorry about even posting this. Moments after I posted, I
    > > realized that all bets are off with 0 with regards to (pointer) type
    > > checking...

    >
    > Well, no, not exactly. There is no need to type-check
    > a NULL, because NULL is a perfectly good value for any kind
    > of pointer at all. The confusion (what remains of it) comes
    > from the fact that there are many ways to spell NULL.
    >
    > Gertrude Stein never said "A NULL is a '\0' is a 0,"
    > but she could have.


    Well, her code was notoriously buggy anyway.




    Brian
     
    Default User, Aug 14, 2007
    #7
  8. matevzb

    CBFalconer Guest

    Eric Sosman wrote:
    >

    .... snip ...
    >
    > Well, no, not exactly. There is no need to type-check
    > a NULL, because NULL is a perfectly good value for any kind
    > of pointer at all. The confusion (what remains of it) comes
    > from the fact that there are many ways to spell NULL.
    >
    > Gertrude Stein never said "A NULL is a '\0' is a 0,"
    > but she could have.


    I don't agree that '\0' is a spelling of NULL. It is a spelling of
    the int value zero. The primes make it precisely an int.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Aug 14, 2007
    #8
  9. matevzb

    somenath Guest

    > - Constant expressions: If the expression is not a
    > compile-time constant, it is not a null pointer
    > constant. fcn(x - x) won't work; you can deduce
    > that the argument will always be zero, but from
    > the compiler's point of view it's just a remarkable
    > coincidence.
    >


    But I compiled the following code using "gcc -Wall " options.It does
    not throw even warning .
    #include <stdio.h>

    void
    fcn (char *str)
    {
    if (str == NULL)
    {
    printf ("str!\n");
    }



    }


    int
    main (void)
    {
    int x;
    fcn (x - x);
    return 0;

    }
     
    somenath, Aug 14, 2007
    #9
  10. matevzb

    Eric Sosman Guest

    somenath wrote On 08/14/07 10:26,:
    >> - Constant expressions: If the expression is not a
    >> compile-time constant, it is not a null pointer
    >> constant. fcn(x - x) won't work; you can deduce
    >> that the argument will always be zero, but from
    >> the compiler's point of view it's just a remarkable
    >> coincidence.
    >>

    >
    >
    > But I compiled the following code using "gcc -Wall " options.It does
    > not throw even warning .
    > #include <stdio.h>
    >
    > void
    > fcn (char *str)
    > {
    > if (str == NULL)
    > {
    > printf ("str!\n");
    > }
    >
    >
    >
    > }
    >
    >
    > int
    > main (void)
    > {
    > int x;
    > fcn (x - x);
    > return 0;
    >
    > }


    <off-topic>

    Try "-Wall -W -O2" or even "-Wall -W -ansi -pedantic -O2".
    Despite its inclusive-sounding name, "-Wall" does not enable
    all of the warnings gcc can generate.

    </off-topic>

    As for the `x - x', the Standard says (6.3.2.3p3):

    An integer constant expression with the value 0,
    or such an expression cast to type void *, is called
    a _null pointer constant._ If a null pointer constant
    is converted to a pointer type, the resulting pointer,
    called a _null pointer,_ is guaranteed to compare
    unequal to a pointer to any object or function.

    The question of whether `x - x' is a null pointer constant
    thus hinges on whether it is an integer constant expression.
    The Standard defines I.C.E. in 6.6p6:

    An _integer constant expression_ shall have integer
    type and shall only have operands that are integer
    constants, enumeration constants, character constants,
    sizeof expressions whose results are integer constants,
    and floating constants that are the immediate operands
    of casts. [...]

    Since `x - x' has operands that are impermissible in an
    I.C.E., `x - x' is not an I.C.E. and hence not an N.P.C.

    --
     
    Eric Sosman, Aug 14, 2007
    #10
  11. matevzb

    Eric Sosman Guest

    CBFalconer wrote On 08/13/07 16:20,:
    > matevzb wrote:
    >
    >>I've ran into some fishy code that, at first glance, is buggy, but
    >>it seems to work correctly and none of the compilers I've tried
    >>(five so far, on various systems) gives any warnings. The code:
    >>
    >>#include <stdio.h>
    >>
    >>void fcn (char *str) {

    >
    >
    > This wants a pointer to char as the parameter.


    Right.

    >> if (str == '\0') {

    >
    >
    > This tests the parameter against a fixed char.


    No, t tests the parameter against a null pointer
    constant.

    >> printf ("str!\n");
    >> }
    >>}
    >>
    >>int main (void) {
    >> fcn ('\0');

    >
    >
    > This passes an integer (char representation) to something that
    > wants a char. pointer.


    No, it passes a NULL-valued `char*' to fcn(). '\0' is
    a zero-valued integer constant expression, hence it is a
    null pointer constant. It is used in a pointer context
    (thanks to the prototype), so the N.P.C. becomes a null
    pointer. The N.P. is converted to type `char*' (again,
    thanks to the prototype) and that value is passed to the
    function.

    >
    >> return 0;
    >>}


    --
     
    Eric Sosman, Aug 14, 2007
    #11
  12. matevzb

    Richard Bos Guest

    CBFalconer <> wrote:

    > Eric Sosman wrote:
    >
    > > Well, no, not exactly. There is no need to type-check
    > > a NULL, because NULL is a perfectly good value for any kind
    > > of pointer at all. The confusion (what remains of it) comes
    > > from the fact that there are many ways to spell NULL.
    > >
    > > Gertrude Stein never said "A NULL is a '\0' is a 0,"
    > > but she could have.

    >
    > I don't agree that '\0' is a spelling of NULL.


    The only spelling of NULL, which is the name of a macro, is NULL. '\0'
    is, however, a spelling of the null pointer constant.

    > It is a spelling of the int value zero.


    No, it's an integer _constant_ of type int and value zero. Big
    difference. An integer constant with value zero is a null pointer
    constant; an int with value zero is not, and is not guaranteed (though
    highly likely) to compare equal to a null pointer object.

    Yes, I do agree that writing NULL when you mean "a null pointer
    constant" is a bad idea, because it adds to the confusion many newbies
    have concerning null pointers.

    > --
    > Chuck F (cbfalconer at maineline dot net)
    > Available for consulting/temporary embedded and systems.
    > <http://cbfalconer.home.att.net>
    >
    >
    >
    > --
    > Posted via a free Usenet account from http://www.teranews.com
    >


    Fix your .sig.

    Richard
     
    Richard Bos, Aug 14, 2007
    #12
  13. CBFalconer wrote:
    > matevzb wrote:
    >>
    >> I've ran into some fishy code that, at first glance, is buggy, but
    >> it seems to work correctly and none of the compilers I've tried
    >> (five so far, on various systems) gives any warnings. The code:
    >>
    >> #include <stdio.h>
    >>
    >> void fcn (char *str) {

    >
    > This wants a pointer to char as the parameter.
    >
    >> if (str == '\0') {

    >
    > This tests the parameter against a fixed char.


    As others have pointed out, '\0' is an allowable null pointer constant, but
    also, even in other contexts, '\0' isn't a char. It's an int. You might be
    thinking of C++, where it would be a char.

    >> printf ("str!\n");
    >> }
    >> }
    >>
    >> int main (void) {
    >> fcn ('\0');

    >
    > This passes an integer (char representation) to something that
    > wants a char. pointer.


    As above, it passes a null pointer constant to a function expecting a
    pointer argument.

    >> return 0;
    >> }
     
    Harald van =?UTF-8?B?RMSzaw==?=, Aug 14, 2007
    #13
  14. matevzb

    Army1987 Guest

    On Mon, 13 Aug 2007 20:04:50 -0400, CBFalconer wrote:

    > Eric Sosman wrote:
    >>

    > ... snip ...
    >>
    >> Well, no, not exactly. There is no need to type-check
    >> a NULL, because NULL is a perfectly good value for any kind
    >> of pointer at all. The confusion (what remains of it) comes
    >> from the fact that there are many ways to spell NULL.
    >>
    >> Gertrude Stein never said "A NULL is a '\0' is a 0,"
    >> but she could have.

    >
    > I don't agree that '\0' is a spelling of NULL. It is a spelling of
    > the int value zero. The primes make it precisely an int.

    Nothing in the standard forbids <stddef.h> or other standard
    headers to #define NULL '\0'.
    --
    Army1987 (Replace "NOSPAM" with "email")
    No-one ever won a game by resigning. -- S. Tartakower
     
    Army1987, Aug 14, 2007
    #14
  15. (Richard Bos) writes:
    > CBFalconer <> wrote:

    [...]
    >> I don't agree that '\0' is a spelling of NULL.

    >
    > The only spelling of NULL, which is the name of a macro, is NULL. '\0'
    > is, however, a spelling of the null pointer constant.


    Of *a* null pointer constant.

    >> It is a spelling of the int value zero.

    >
    > No, it's an integer _constant_ of type int and value zero. Big
    > difference. An integer constant with value zero is a null pointer
    > constant;


    Right. More generally, so is an integer constant expression with
    value zero.

    > an int with value zero is not, and is not guaranteed (though
    > highly likely) to compare equal to a null pointer object.


    An "int with value zero" presumably refers to an object that exists
    during execution time. A null pointer constant exists only in source
    code; there's no such thing during execution.

    An int with value zero cannot compare equal (or unequal) to a pointer
    whose value is a null pointer. An attempt to compare them is a
    constraint violation, because the types are incompatible.

    It's likely, but not guaranteed, that *converting* a (non-constant)
    int with value zero to a pointer type will yield a null pointer value.
    Since integer-to-pointer conversion is implementation-defined, this is
    actually independent of the question of whether a null pointer value
    is represented as all-bits-zero.

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 14, 2007
    #15
  16. matevzb

    Richard Bos Guest

    Keith Thompson <> wrote:

    > (Richard Bos) writes:
    > > CBFalconer <> wrote:

    > [...]
    > >> I don't agree that '\0' is a spelling of NULL.

    > >
    > > The only spelling of NULL, which is the name of a macro, is NULL. '\0'
    > > is, however, a spelling of the null pointer constant.

    >
    > Of *a* null pointer constant.


    _The_ spelling of _a_ null pointer constant, or _a_ spelling of _the_
    null pointer constant. Take your pick; IMO, the difference is of an
    angels-on-needlepoints value.

    Richard
     
    Richard Bos, Aug 15, 2007
    #16
  17. (Richard Bos) writes:
    > Keith Thompson <> wrote:
    >> (Richard Bos) writes:
    >> > CBFalconer <> wrote:

    >> [...]
    >> >> I don't agree that '\0' is a spelling of NULL.
    >> >
    >> > The only spelling of NULL, which is the name of a macro, is NULL. '\0'
    >> > is, however, a spelling of the null pointer constant.

    >>
    >> Of *a* null pointer constant.

    >
    > _The_ spelling of _a_ null pointer constant, or _a_ spelling of _the_
    > null pointer constant. Take your pick; IMO, the difference is of an
    > angels-on-needlepoints value.


    It's not a big deal, but there isn't a single null pointer constant;
    there are arbitrarily many of them.

    An integer constant expression with the value 0, or such an
    expression cast to type void *, is called a _null pointer
    constant_.

    Referring to "the null pointer constant" makes no more sense than
    referring to "the integer constant". (A difference is that all null
    pointer constants refer to the same value, but they're still distinct
    constants.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 15, 2007
    #17
  18. Eric Sosman wrote:
    >
    > matevzb wrote On 08/13/07 14:30,:

    [...]
    > > Yes, sorry about even posting this. Moments after I posted, I realized
    > > that all bets are off with 0 with regards to (pointer) type
    > > checking...

    >
    > Well, no, not exactly. There is no need to type-check
    > a NULL, because NULL is a perfectly good value for any kind
    > of pointer at all. The confusion (what remains of it) comes
    > from the fact that there are many ways to spell NULL.
    >
    > Gertrude Stein never said "A NULL is a '\0' is a 0,"
    > but she could have.


    A NULL by any other name would smell as sweet to those nasal demons.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Aug 16, 2007
    #18
    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:
    10
    Views:
    745
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    876
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    11
    Views:
    518
    Richard Bos
    May 8, 2008
  4. asker123
    Replies:
    0
    Views:
    1,358
    asker123
    Jun 27, 2009
  5. Replies:
    4
    Views:
    1,333
    Fred Zwarts
    Jul 2, 2009
Loading...

Share This Page