Atoi function (char * p)

I need to convert my string to an int array. so I do a loop on the line and call the int atoi(char *p)function from stdlib.h, but it does not work on mac (several times ago I tried it on Windows, and it works well). simple loop:

    for(p = buff; *p; p++)
    printf("%d", atoi(p));

What is the problem with the operation of this function, and you think?

+3
source share
1 answer

The function works fine. For example, if it buffcontains "123", your code will print 123233.

However, it looks like you just want to print the value of the individual digits, and in this case atoihas nothing to do with your problem. Try:

for(p = buff; *p; p++)
    printf("%d", *p-'0');
+5
source

All Articles