Re: a question abou "atoi"



On Oct 30, 5:17 pm, 66650...@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? I only want to convert string[2],not other string[i].

I've written these sentences:

        int a;
        char string[7]="111111";

        a=atoi(string[3]);

however,the compiler said:
error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const
char *'
        Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast.

#include <string.h>
#include <stdio.h>

int main(void)
{
int a;
char string[7] = "123456";
char substring[2] = {0}; /* now contains [0][0] */
int index;

for (index = 0; index < strlen(string); index++) {
substring[0] = string[index];
a = atoi(substring);
printf("%d\n", a);
}
return 0;
}

.



Relevant Pages