Re: a question abou "atoi"
- From: Mark L Pappin <mlp@xxxxxxx>
- Date: Fri, 31 Oct 2008 12:21:10 +1000
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].
'string[2]' is not a string; it is a single character.
The array named 'string' contains a string, initialized as if by
char string[20] = {'1', '2', '3', '4', '5', '\0',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Note: you should not use 'string', or any other identifier beginning
with 'str', as the name of any object or function you define, because
almost all such names are reserved for the use of the implementor,
which you are not.
A string consists of a sequence of zero or more characters followed by
a null character. You need to pass a pointer to such a sequence to
'atoi()'.
Note also: you should probably not use 'atoi()' in any case as it has
a design limitation which has been discussed here recently; use
'strtol()' instead, as it can report its results in more detail.
I'm eager to find an solution.thinks,thinks,thinks!!
To extract the decimal value of a single digit from within a string
such as that shown above:
int value = string[2] - '0';
'value' now contains the value 3. This works because the Standard
guarantees that the representations of the characters '0' through '9'
are sequential. It makes no such guarantee about non-digits, so you
can not portably assume that
('a' + 2) == 'c'
for example.
mlp
.
- Prev by Date: Re: A question about how to enter a very long number.
- Next by Date: Re: Judge the encode systm used by the file.
- Previous by thread: Re: a question abou "atoi"
- Next by thread: Re: a question abou "atoi"
- Index(es):
Relevant Pages
|