Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Clarify atol statment

Reply
Thread Tools

Clarify atol statment

 
 
sapnsapn@hotmail.com
Guest
Posts: n/a
 
      07-07-2005
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

 
Reply With Quote
 
 
 
 
Sensei
Guest
Posts: n/a
 
      07-07-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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:

senseiublic$ 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-darwin
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
senseiublic$ ./a
result is 0
senseiublic$

#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?
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-07-2005
(E-Mail Removed) 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
(E-Mail Removed)lid
 
Reply With Quote
 
Cris Dunbar
Guest
Posts: n/a
 
      07-08-2005
(E-Mail Removed) 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
 
Reply With Quote
 
sapnsapn@hotmail.com
Guest
Posts: n/a
 
      07-08-2005
Thanks for taking time to investigate, Cris! The soft-hyphen you found
was a part of Google-ification, I guess!

Regards,
Sapn

 
Reply With Quote
 
sapnsapn@hotmail.com
Guest
Posts: n/a
 
      07-08-2005
Thanks for taking time to investigate, Cris! The soft-hyphen you found
was a part of Google-ification, I guess!

Regards,
Sapn

 
Reply With Quote
 
sapnsapn@hotmail.com
Guest
Posts: n/a
 
      07-08-2005
- 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!

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-08-2005
(E-Mail Removed) 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
(E-Mail Removed)lid

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-08-2005
(E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: difference between casting and atol,atoi functions infobahn C Programming 1 02-15-2005 08:09 AM
Re: difference between casting and atol,atoi functions Olaf C Programming 0 02-15-2005 06:56 AM
string.atoi and string.atol broken? Mike Moum Python 19 01-27-2005 08:35 AM
what are the reverse functions of atof( ), atoi( ) and atol( ) ? Sharon C Programming 4 11-17-2003 10:13 AM
converting a char* to long using atol does not work :( Gizmo C++ 5 09-08-2003 07:25 PM



Advertisments