Re: a question abou "atoi"



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



Relevant Pages

  • Re: atoi query
    ... have noticed the problem with your argument to atoi. ... strtol) must be supplied with a string, not a character, for converting ... lineis not a string, ... a sequence of characters terminated by the first null character. ...
    (comp.lang.c)
  • Re: atoi
    ... That;'s how atoi is defined to work. ... character. ... But if you want to allow spaces in the string, it can't tell the difference between _12_, ... An example of this is my numeric edit control on my MVP Tips ...
    (microsoft.public.vc.mfc)
  • Re: atoi
    ... That;'s how atoi is defined to work. ... character. ... But if you want to allow spaces in the string, it can't tell the difference between _12_, ... An example of this is my numeric edit control on my MVP Tips ...
    (microsoft.public.vc.mfc)
  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)