Re: a question abou "atoi"



Kelsey Bjarnason wrote:
On Fri, 31 Oct 2008 11:23:23 +0000, Mark McIntyre wrote:

66650755@xxxxxx wrote:
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?
You don't need to - string[2] is already an integer type.

int x = string[2];

does the trick.

True, but the value I get here when doing this is 51; I suspect he wanted the value 3.

int x = string[2] - '0'; would do the trick.

I was kinda leaving that as an exercise for the reader, given that its an FAQ... :-)

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
.



Relevant Pages