strtol()

Discussion in 'C++' started by Pavel Krcmar, Sep 1, 2004.

  1. Pavel Krcmar

    Pavel Krcmar Guest

    Hi,
    I'am a little bit confused. I tried this snippet (below) and ( endptr !=
    NULL ) is true everytime. I was looking to
    http://www.mkssoftware.com/docs/man3/strtol.3.asp
    and then to
    http://www.cplusplus.com/ref/cstdlib/strtol.html
    The second www specifies things like they are done - endptr is not NULL.
    Is something wrong?

    Best Regards,
    Pavel Krcmar

    ---------------------------------
    int main()
    {
    int i;
    const char *z = "345";


    char *endptr;

    i = strtol( z, &endptr, 10 );
    if ( endptr != NULL )
    {
    printf("endptr is not zero!\n");
    printf("%s\n", endptr );
    }
    printf( "i=%i\n", i );
    return 0;
    }
    ---------------------------------
     
    Pavel Krcmar, Sep 1, 2004
    #1
    1. Advertising

  2. Pavel Krcmar

    Sam Holden Guest

    On Wed, 01 Sep 2004 10:42:25 +0200,
    Pavel Krcmar <> wrote:
    > Hi,
    > I'am a little bit confused. I tried this snippet (below) and ( endptr !=
    > NULL ) is true everytime. I was looking to
    > http://www.mkssoftware.com/docs/man3/strtol.3.asp
    > and then to
    > http://www.cplusplus.com/ref/cstdlib/strtol.html
    > The second www specifies things like they are done - endptr is not NULL.
    > Is something wrong?


    endptr simply gets set to point to the first character in the string
    that is not part of the number. It will never be set to NULL, if
    you pass NULL instead of an actual pointer to a char* then this
    won't happen.


    > Best Regards,
    > Pavel Krcmar
    >
    > ---------------------------------
    > int main()
    > {
    > int i;
    > const char *z = "345";
    >
    >
    > char *endptr;
    >
    > i = strtol( z, &endptr, 10 );
    > if ( endptr != NULL )
    > {
    > printf("endptr is not zero!\n");
    > printf("%s\n", endptr );
    > }
    > printf( "i=%i\n", i );
    > return 0;
    > }
    > ---------------------------------


    endptr will never equal NULL in that code, since it will be
    set to point to the trailing '\0' in "345" by strtol.

    --
    Sam Holden
     
    Sam Holden, Sep 1, 2004
    #2
    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. Amadeus W.M.

    strtol exceptions

    Amadeus W.M., Oct 4, 2004, in forum: C++
    Replies:
    6
    Views:
    468
    Pete Becker
    Oct 5, 2004
  2. William Payne

    Sufficient error checking of strtol()?

    William Payne, Feb 2, 2004, in forum: C Programming
    Replies:
    5
    Views:
    1,337
    AJ Mohan Rao
    Feb 3, 2004
  3. Richard Bos

    strtol question

    Richard Bos, Feb 12, 2004, in forum: C Programming
    Replies:
    11
    Views:
    728
    Richard Bos
    Feb 13, 2004
  4. Peter Dunker

    strtol with hex greater than 4 byte ?

    Peter Dunker, Apr 29, 2004, in forum: C Programming
    Replies:
    10
    Views:
    1,306
    Peter Nilsson
    Apr 30, 2004
  5. David Scarlett

    strtol library function

    David Scarlett, May 15, 2004, in forum: C Programming
    Replies:
    16
    Views:
    864
    Dan Pop
    May 18, 2004
Loading...

Share This Page