Clarify atol statment

Discussion in 'C Programming' started by sapnsapn@hotmail.com, Jul 7, 2005.

  1. Guest

    There is a statement in c code I am reviewing

    filenum = atoll(item->d_name + strlen(msgid_with_append_str) + 1);

    In a certain snapshot,
    item->d_name = 050707143827.AAAA.11810.00000001
    msgid_with_append_str = 050707143827.AAAA.11810
    strlen(msgid_with_append_str) = 23

    resulting in,
    filenum = 0

    Can anyone explain what is going on? I am not a C programmer by trade.
    Thanks.

    Regards,
    Sapn
    , Jul 7, 2005
    #1
    1. Advertising

  2. Sensei Guest

    wrote:
    > There is a statement in c code I am reviewing
    >
    > filenum = atoll(item->d_name + strlen(msgid_with_append_str) + 1);


    atoll: ascii to long long.

    > In a certain snapshot,
    > item->d_name = 050707143827.AAAA.11810.00000001


    So item->d_name... is it a char*?

    > msgid_with_append_str = 050707143827.AAAA.11810


    char*?

    > strlen(msgid_with_append_str) = 23


    This is a numeral, as it should be.

    > resulting in,
    > filenum = 0
    >
    > Can anyone explain what is going on? I am not a C programmer by trade.
    > Thanks.


    I suppose it compiles, you say it results in 0. I would say it has to
    give an error, but it doesn't:

    sensei:public$ gcc -Wall -v -std=c99 -pedantic -o a a.c
    Using built-in specs.
    Target: powerpc-apple-darwin8
    Configured with: /private/var/tmp/gcc/gcc-5026.obj~19/src/configure
    --disable-checking --prefix=/usr --mandir=/share/man
    --enable-languages=c,objc,c++,obj-c++
    --program-transform-name=/^[cg][^+.-]*$/s/$/-4.0/
    --with-gxx-include-dir=/include/gcc/darwin/4.0/c++
    --build=powerpc-apple-darwin8 --host=powerpc-apple-darwin8
    --target=powerpc-apple-darwin8
    Thread model: posix
    gcc version 4.0.0 (Apple Computer, Inc. build 5026)
    /usr/libexec/gcc/powerpc-apple-darwin8/4.0.0/cc1 -quiet -v
    -D__DYNAMIC__ a.c -fPIC -quiet -dumpbase a.c -auxbase a -Wall -pedantic
    -std=c99 -version -o /var/tmp//ccB0NHW5.s
    ignoring nonexistent directory
    "/usr/lib/gcc/powerpc-apple-darwin8/4.0.0/../../../../powerpc-apple-darwin8/include"
    #include "..." search starts here:
    #include <...> search starts here:
    /usr/local/include
    /usr/lib/gcc/powerpc-apple-darwin8/4.0.0/include
    /usr/include
    /System/Library/Frameworks
    /Library/Frameworks
    End of search list.
    GNU C version 4.0.0 (Apple Computer, Inc. build 5026)
    (powerpc-apple-darwin8)
    compiled by GNU C version 4.0.0 (Apple Computer, Inc. build 5026).
    GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
    as -arch ppc -o /var/tmp//ccRrT9Ac.o /var/tmp//ccB0NHW5.s
    /usr/libexec/gcc/powerpc-apple-darwin8/4.0.0/collect2 -dynamic -arch
    ppc -weak_reference_mismatches non-weak -o a -lcrt1.o
    /usr/lib/gcc/powerpc-apple-darwin8/4.0.0/crt2.o
    -L/usr/lib/gcc/powerpc-apple-darwin8/4.0.0
    -L/usr/lib/gcc/powerpc-apple-darwin8/4.0.0
    -L/usr/lib/gcc/powerpc-apple-darwin8/4.0.0/../../.. /var/tmp//ccRrT9Ac.o
    -lgcc -lgcc_eh -lSystemStubs -lmx -lSystem
    sensei:public$ ./a
    result is 0
    sensei:public$

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>

    int main(void)
    {
    char *d_name = "Hello";
    char *msg_id = "world!";

    long long i;

    i = atoll(d_name + strlen(msg_id) + 1);

    printf("result is %lld\n", i);

    return 0;
    }

    Misfunctioning? char* + size_t + 1 (implicit int?) gets converted to a
    char* and so the memory location somehow gets to 0?
    Sensei, Jul 7, 2005
    #2
    1. Advertising

  3. Eric Sosman Guest

    wrote:
    > There is a statement in c code I am reviewing
    >
    > filenum = atoll(item->d_name + strlen(msgid_with_append_str) + 1);
    >
    > In a certain snapshot,
    > item->d_name = 050707143827.AAAA.11810.00000001
    > msgid_with_append_str = 050707143827.AAAA.11810
    > strlen(msgid_with_append_str) = 23
    >
    > resulting in,
    > filenum = 0
    >
    > Can anyone explain what is going on? I am not a C programmer by trade.


    It looks like the code ought to work, and produce the
    value 1 as the result. Two things to check:

    - Has <stdlib.h> been #include'd? If it has not, atoll()
    may not have been declared correctly. (A C99 compiler
    would complain about this, but a pre-C99 compiler that
    accepts `long long' as an extension might not.)

    - How do you know that `filenum' is zero? If you are
    using one of the printf() functions, note that the
    conversion specifier for a `long long' is "%lld", not
    "%d" or "%ld".

    If neither of these leads to a solution, please post an
    actual program (complete, compilable, and minimal) that
    demonstrates the difficulty, along with its output.

    --
    Eric Sosman
    lid
    Eric Sosman, Jul 7, 2005
    #3
  4. Cris Dunbar Guest

    wrote:
    > There is a statement in c code I am reviewing
    >
    > filenum = atoll(item->d_name + strlen(msgid_with_append_str) + 1);
    >
    > In a certain snapshot,
    > item->d_name = 050707143827.AAAA.11810.00000001
    > msgid_with_append_str = 050707143827.AAAA.11810
    > strlen(msgid_with_append_str) = 23
    >
    > resulting in,
    > filenum = 0
    >
    > Can anyone explain what is going on? I am not a C programmer by trade.
    > Thanks.
    >
    > Regards,
    > Sapn
    >


    I can't reproduce the same thing here at home, but earlier today
    I noticed something interesting. From Google groups, I cut and
    pasted your message into a text editor, and a hyphen showed up
    between the first 6 zeroes and the last two digits at the end of
    item->d_name like so:

    item->d_name = 050707143827.AAAA.11810.000000-01

    When I viewed the html source for the page, the character showed
    up as &#173, which is HTML for a "soft hyphen"?

    It could be an artefact of viewing the message through Google,
    and I could be totally off base, but if that character is really
    in there, then of course your atol will only see the zeroes and
    will stop converting on the invisible hyphen.

    Cris
    Cris Dunbar, Jul 8, 2005
    #4
  5. Guest

    Thanks for taking time to investigate, Cris! The soft-hyphen you found
    was a part of Google-ification, I guess!

    Regards,
    Sapn
    , Jul 8, 2005
    #5
  6. Guest

    Thanks for taking time to investigate, Cris! The soft-hyphen you found
    was a part of Google-ification, I guess!

    Regards,
    Sapn
    , Jul 8, 2005
    #6
  7. Guest

    - How do you know that `filenum' is zero? If you are using one of the
    printf() functions, note that the conversion specifier for a `long
    long' is "%lld", not "%d" or "%ld".

    That's it! I was using "%d". Using "%lld" showed me filenum has a value
    2130640639. Thanks, Eric!
    , Jul 8, 2005
    #7
  8. Eric Sosman Guest

    wrote:

    > - How do you know that `filenum' is zero? If you are using one of the
    > printf() functions, note that the conversion specifier for a `long
    > long' is "%lld", not "%d" or "%ld".
    >
    > That's it! I was using "%d". Using "%lld" showed me filenum has a value
    > 2130640639. Thanks, Eric!


    You're welcome, but I don't think "it" has been found
    yet. From the information in your original post the answer
    ought to have been 1 -- in a sense, the 0 you used to get
    is "closer to correct" than the two billion you're now
    getting ...

    --
    Eric Sosman
    lid
    Eric Sosman, Jul 8, 2005
    #8
  9. writes:
    > - How do you know that `filenum' is zero? If you are using one of the
    > printf() functions, note that the conversion specifier for a `long
    > long' is "%lld", not "%d" or "%ld".
    >
    > That's it! I was using "%d". Using "%lld" showed me filenum has a value
    > 2130640639. Thanks, Eric!


    If you must post through groups.google.com, please quote and attribute
    properly.

    As we've written countless times:

    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    --
    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.
    Keith Thompson, Jul 8, 2005
    #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. Gizmo
    Replies:
    5
    Views:
    417
    llewelly
    Sep 8, 2003
  2. Sharon
    Replies:
    4
    Views:
    968
    CBFalconer
    Nov 17, 2003
  3. Mike Moum

    string.atoi and string.atol broken?

    Mike Moum, Jan 25, 2005, in forum: Python
    Replies:
    19
    Views:
    664
    Peter Otten
    Jan 27, 2005
  4. Olaf
    Replies:
    0
    Views:
    611
  5. infobahn
    Replies:
    1
    Views:
    328
    Randy Howard
    Feb 15, 2005
Loading...

Share This Page