So as we all probably know, the atoi
converts a char to a number. But, what do you do if you only want one of the array elements instead of the whole array?
Please look at the following:
for (h = 0; h < 5; h++)
{
num[h] = atoi(temp[h]);
}
Assume that num is an array of type int
and that temp is and array of type char
. This gives me one of those annoying conversion problems:
Invalid conversion from 'char' to 'const char *'
Any suggestions on how to convert a single element of a char array to an int using atoi?
Besides just using its integral value as shown by James, you could put it in a seperate buffer:
If you only want to convert a single character you don't need to use
atoi()
:In C, the value of each digit is one greater than the value of the previous digit, so this is guaranteed to work.
The reason that
atoi()
does not work is because it takes aconst char*
as its argument, not achar
. That pointer has to point to a null terminated string.