Help understanding const char* operators in c++
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; char handFloor; 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; char handFloor;
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.
strncpy(handCeil, handText, index - handText); strcpy(handFloor, index + 1);
is equivalent to
int index = handText.indexOf('-'); // I changed this for you handCeil = handText.substring(0, index+1); handFloor = handText.substring(index+1);
So it splits the string by the '-' and puts the first part (including the '-' itself, I think) into handCeil and the remainder into handFloor.
index - handText means this: index points to a specific character, handText points 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 '-'. strncpy copies n bytes, so if index points to the 3rd character it will copy 3 characters. index + 1 means point to the character 1 after the one pointed to by index.