Re: (part 5) Han from China answers your C questions



Borked Pseudo Mailed <nobody@xxxxxxxxxxxxxxxxx> writes:

a question abou "atoi"
66650...@xxxxxx said:
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].

int a;
char string[20]="12345";

atoi((a=string[2]-'0',"X"));

The consensus is that this exhibits undefined behaviour (quite apart
from being silly). If the OP really needs to use atoi to do this
simple conversion, the easiest way would probably be:

a = atoi((char []){string[2], 0});

--
Ben.
.



Relevant Pages

  • Re: The Year 2038 Problem
    ... "Generic Usenet Account" wrote in message ... You mean systems which use a signed 32-bit int for time_t. ... There already is a solution, consensus for it, and implementations of it. ...
    (comp.lang.c)
  • Re: The Year 2038 Problem
    ... "Generic Usenet Account" wrote in message ... You mean systems which use a signed 32-bit int for time_t. ... There already is a solution, consensus for it, and implementations of it. ...
    (comp.programming)
  • Re: The Year 2038 Problem
    ... "Generic Usenet Account" wrote in message ... You mean systems which use a signed 32-bit int for time_t. ... There already is a solution, consensus for it, and implementations of it. ...
    (comp.unix.misc)
  • Re: Stack and Queue
    ... int main ... As for the Queue FIFO logic.So the item which is stored first is ... It exhibits undefined behaviour because you ... are calling variadic functions without a valid function prototype in scope. ...
    (comp.lang.c)
  • Re: Plz explain me the following code
    ... int x=20,y=35; ... exhibits undefined behaviour. ... Dig out the comp.lang.c FAQ for details ...
    (comp.lang.c)