I need to translate some code from c++ to java, and I am not so sure how to deal with the ‘const char*’ operators. Check code below:
const char* handText;
const char* index = strchr(handText, '-');
char handCeil[4];
char handFloor[4];
strncpy(handCeil, handText, index - handText);
strcpy(handFloor, index + 1);
What I got is something like:
String handText;
String index = handText.substring(handText.indexOf('-'));
char handCeil[4];
char handFloor[4];
However, I don’t know what it means when you add integers (index + 1), or when you subtract (index – handText) strings in c++. Sorry if this is a stupid question, but I never learned how to program in c++, only in java. Thank you.
This
is equivalent to
So it splits the string by the ‘-‘ and puts the first part (including the ‘-‘ itself, I think) into
handCeiland the remainder intohandFloor.index - handTextmeans this:indexpoints to a specific character,handTextpoints to the beginning of that string. If you subtract the two then you get the number of characters between those two pointers, or the array index of the first ‘-‘.strncpycopiesnbytes, so ifindexpoints to the 3rd character it will copy 3 characters.index + 1means point to the character 1 after the one pointed to byindex.