I have a function for converting a numerical String to an int
int Str2int (String Str_value) {
char buffer[2]; //max length is thre units
char str_value = Str_value.toCharArray(buffer, 3);
int int_value = atoi(str_value);
return int_value;
}
it giving me this error:
_09.cpp: In function 'int Str2int(String)':
_09:14: error: void value not ignored as it ought to be
_09:15: error: invalid conversion from 'char' to 'const char*'
_09:15: error: initializing argument 1 of 'int atoi(const char*)'
The docs for toCharArray() are incorrect. I think this should take care of it for you,
int Str2int (String Str_value)
{
char buffer[3]; //max length is three units
Str_value.toCharArray(buffer, 3);
int int_value = atoi(buffer);
return int_value;
}
I might speak my mind prematurely but I really do not think you should copy a String object when passing it to functions. If the string is long enough (or the available stack is 'small') that will cause a serious headache. At least when you think about how easy a remedy to the situation is. Simply add an & before the variable name, but BE ADVISED the changes you make to the string inside the function is also made to the string outside of the function, because with the & there is no longer a distinction. You pass the address of the object, not the entire object
(an address is two bytes, an object might be orders of magnitude larger than that).
Good luck!
The buffer needs to be large enough to hold the trailing NULL that is the end-of-character marker for the string. If the String contains "123" the whole String will not be copied into buffer by toCharArray, if buffer is only 3 elements long.
The buffer needs to be large enough to hold the trailing NULL that is the end-of-character marker for the string. If the String contains "123" the whole String will not be copied into buffer by toCharArray, if buffer is only 3 elements long.
The buffer needs to be large enough to hold the trailing NULL that is the end-of-character marker for the string. If the String contains "123" the whole String will not be copied into buffer by toCharArray, if buffer is only 3 elements long.