problematic atoi conversion

C

cerr

Hi There,

I'm experiencing a weird problem with atoi and i'm looking for a
solution asap.
Okay, what I have in my code:

prs_log(LOG_NOTICE,"DEBUG: tmp:%s strlen:%d tmp[0]:%c tmp[1]:%c",tmp,
strlen(tmp), tmp[0], tmp[1]);
seq == atoi(tmp); //seq
prs_log(LOG_NOTICE,"DEBUG: parsed seq:%d",seq);

the prs_log function writes the message to syslog-ng.
Okay, now the output i'm getting looks like this:

DEBUG: tmp:51 strlen:2 tmp[0]:5 tmp[1]:1
DEBUG: parsed seq:0

What's going on here? I don't understand...:eek: i did the strlen thingy
in the first string to see if there's any unprintable characters or
similar in my string...
I'm stuck, any help or tip is appreciated!

Thank you!
Ron
 
C

cerr

Hi There,

I'm experiencing a weird problem with atoi and i'm looking for a
solution asap.
Okay, what I have in my code:

prs_log(LOG_NOTICE,"DEBUG: tmp:%s strlen:%d tmp[0]:%c tmp[1]:%c",tmp,
strlen(tmp), tmp[0], tmp[1]);
seq == atoi(tmp);                        //seq
prs_log(LOG_NOTICE,"DEBUG: parsed seq:%d",seq);

the prs_log function writes the message to syslog-ng.
Okay, now the output i'm getting looks like this:

DEBUG: tmp:51 strlen:2 tmp[0]:5 tmp[1]:1
DEBUG: parsed seq:0

What's going on here? I don't understand...:eek: i did the strlen thingy
in the first string to see if there's any unprintable characters or
similar in my string...
I'm stuck, any help or tip is appreciated!

Thank you!
Ron

Okay, okay....just found it! Hahaha that's kinda embarassing... == is
wrong of course!
lol
 
K

Keith Thompson

cerr said:
I'm experiencing a weird problem with atoi and i'm looking for a
solution asap.
Okay, what I have in my code:

prs_log(LOG_NOTICE,"DEBUG: tmp:%s strlen:%d tmp[0]:%c tmp[1]:%c",tmp,
strlen(tmp), tmp[0], tmp[1]);
seq == atoi(tmp); //seq
prs_log(LOG_NOTICE,"DEBUG: parsed seq:%d",seq);

You've already found the primary problem, using "==" rather than "=".

Assuming that prs_log() does printf-style formatting, be aware that
"%d" expects an int argument, but strlen() returns a size_t result.
On some systems, you can get away with this; on others, you might
get nonsense results, or worse. The safest way to handle this is,
for example:

printf("strlen(s) = %lu\n", (unsigned long)strlen(s));

If your implementation supports the new C99 format specifiers,
there's a specific format for size_t:

printf("strlen(s) = %zu\n", strlen(s));

Also, the atoi() function performs essentially no error checking,
and can even invoke undefined behavior if the argument string either
doesn't represent a number or is out of range. The standard is
not entirely clear about just when the behavior is undefined.

For a quick-and-dirty program, or when you're certain that the string
represents an integer within the range INT_MIN..INT_MAX, you can get
away with using atoi(). For safety and flexibility, use strtol(),
though it's more complicated to use.
 

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

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top