Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atoi(char *p) function

Tags:

c

xcode

macos

I need to convert my string into int array. so i make a loop over string, and call int atoi(char *p) function from stdlib.h, but it doesn`t working on mac (some times ago i tryed it on Windows, and it workong well). simple loop:

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

whats the problem with work of this function, u think?

like image 973
Vitaly Davydov Avatar asked Sep 22 '26 16:09

Vitaly Davydov


1 Answers

The function is working perfectly fine. For instance if buff contains "123", your code will print 123233.

However it seems you want to just print the value of individual digits, in which case atoi has nothing to do with your problem. Try:

for(p = buff; *p; p++)
    printf("%d", *p-'0');
like image 188
R.. GitHub STOP HELPING ICE Avatar answered Sep 24 '26 05:09

R.. GitHub STOP HELPING ICE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!