Re: a question abou "atoi"
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 30 Oct 2008 18:32:45 -0700
66650755@xxxxxx writes:
First,thanks for all who have answered my last question.
if char string[20]="12345";
how could I convert the string[2](that is "3") to an int by using
atoi? I only want to convert string[2],not other string[i].
atoi is dangerous; avoid it. It can invoke undefined behavior on
error, i.e., arbitrarily bad things can happen. We had a discussion
here recently about what kinds of errors, overflow vs. ill-formed
strings, can cause what consequences, but the bottom line is that it
can't be used safely unless you carefully check the argument first.
strtol() can be a bit harder to use, but it's much easier to use
safely.
I've written these sentences:
C doesn't have "sentences".
int a;
char string[7]="111111";
a=atoi(string[3]);
however,the compiler said:
error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const
char *'
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast.
The error message implies that you're using a C++ compiler. Decide
which language you want to use, and post to the appropriate group.
Most C++ compilers can be invoked as C compilers; if you want to write
C, find out how to do this for yours. (Sometimes naming your source
file with a ".c" suffix is sufficient.)
A string is a sequence of characters, terminated by and including a
terminating null character ('\0'). A single character isn't a string
(unless it's a '\0', but that's not useful here). atoi() expects a
pointer to a string; passing it a single character doesn't make sense.
Ignore what it says about converting to a pointer type; that's not
what you want to do.
If you really want to extract a single character from a string and
pass it *as a string* to atoi (or, preferably, to strtol), you could
declare a 2-character array, copy the desired character to the first
element, and set the second element to '\0'. The contents of the
array are now a valid string, and you can pass it (or, rather, a
pointer to it) to atoi or strtol.
But there's an easier way. Since the language guarantees that, for
whatever character set you're using, the digits '0' through '9' have
contiguous representations, the following are guaranteed:
'0' - '0' == 0
'1' - '0' == 1
'2' - '0' == 2
...
'9' - '0' == 9
The value of '0' is most likely 48 on your system, or it might be 240
if you're using an IBM mainframe, but the above are still guaranteed.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Prev by Date: (part 5) Han from China answers your C questions
- Next by Date: Re: A question about how to enter a very long number.
- Previous by thread: (part 5) Han from China answers your C questions
- Next by thread: Re: a question abou "atoi"
- Index(es):
Relevant Pages
|