C++ – atoi() conversion error

atoic++

atoi() is giving me this error:


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

from this line:
int pid = atoi( token.at(0) );
where token is a vector

how can i go around this?

Best Solution

token.at(0) is returning a single char, but atoi() is expecting a string (a pointer to a char.) Either convert the single character to a string, or to convert a single digit char into the number it represents you can usually* just do this:

int pid = token.at(0) - '0';

* The exception is when the charset doesn't encode digits 0-9 in order which is extremely rare.

Related Question