Convert single char to int

后端 未结 3 1965
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 21:00

How can I convert char a[0] into int b[0] where b is a empty dynamically allocated int array

I have tried

char a[] = \"4x^0\";
int *b;
b = new int[10         


        
3条回答
  •  粉色の甜心
    2020-12-16 21:56

    The atoi() version isn't working because atoi() operates on strings, not individual characters. So this would work:

    char a[] = "4";
    b[0] = atoi(a);
    

    Note that you may be tempted to do: atoi(&temp) but this would not work, as &temp doesn't point to a null-terminated string.

提交回复
热议问题