How to convert a string to integer in C?

后端 未结 12 1577
清酒与你
清酒与你 2020-11-22 01:40

I am trying to find out if there is an alternative way of converting string to integer in C.

I regularly pattern the following in my code.

char s[] =         


        
12条回答
  •  無奈伤痛
    2020-11-22 01:47

    Ok, I had the same problem.I came up with this solution.It worked for me the best.I did try atoi() but didn't work well for me.So here is my solution:

    void splitInput(int arr[], int sizeArr, char num[])
    {
        for(int i = 0; i < sizeArr; i++)
            // We are subtracting 48 because the numbers in ASCII starts at 48.
            arr[i] = (int)num[i] - 48;
    }
    

提交回复
热议问题