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

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



Relevant Pages

  • Re: user defined function that converts string to float
    ... > I need user defined function that converts string to float in c. ... initial, possibly empty, sequence of white-space characters (as ... point character, then an optional exponent part as defined in ... then a nonempty sequence of hexadecimal digits ...
    (comp.lang.c)
  • Re: Check for Common character sequence ( I will pay)?
    ... Dude, programming is all problem-solving. ... You need to identify character sequences of 3 or more characters that appear ... in more than one string. ... and test each 3-character sequence that results. ...
    (microsoft.public.dotnet.framework)
  • Re: Check for Common character sequence ( I will pay)?
    ... Do I need to return an array? ... You need to identify character sequences of 3 or more characters that appear ... in more than one string. ... and test each 3-character sequence that results. ...
    (microsoft.public.dotnet.framework)
  • Re: Check for Common character sequence ( I will pay)?
    ... Yes you are returning an array of FoundString objects. ... in more than one string. ... This means that you have to identify sequences 1 character at a time, ... Again, obviously, if the 3-character sequence doesn't match, neither will ...
    (microsoft.public.dotnet.framework)
  • [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)