In article <11**********************@f14g2000cwb.googlegroups .com>,
<ro*********@gmail.com> wrote:
I have a string
strcpy(str, "2A");
and I want to grab the char 2 and convert it to an integer.
I tried doing this
int num = atoi(str[0]);
and it will not compile.
atoi() must be passed a const char *str, but str[0] is just a plain char.
Even with automatic promotion into a const char, that's still a difference
of char vs pointer-to-char .
What is your general rule for such inputs? That there is -exactly-
one digit, which will be followed by something that is not a digit?
That you only want to convert one digit no matter what follows
(including possibly another digit)? That there might be several
digits, followed by something that is not a digit?
atoi() stops parsing when it finds something that isn't a valid
digit... but watch out for the boundary case where the input
does not start start with a digit. See also strtoul() and kin,
which have much better error reporting.
If you have exactly one digit, you could just use
int num = str[0]-'0';
--
All is vanity. -- Ecclesiastes