Clarify atol statment

S

sapnsapn

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
 
S

Sensei

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?
 
E

Eric Sosman

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.
 
C

Cris Dunbar

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
 
S

sapnsapn

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

Regards,
Sapn
 
S

sapnsapn

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

Regards,
Sapn
 
S

sapnsapn

- 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!
 
E

Eric Sosman

- 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 ...
 
K

Keith Thompson

- 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top