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[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.

Answers


This

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.


Need Your Help

Back to basics; for-loops, arrays/vectors/lists, and optimization

c# arrays optimization list performance

I was working on some code recently and came across a method that had 3 for-loops that worked on 2 different arrays.

Proper way to filter a table using values in another table in MS Access?

database performance ms-access

I have a table of transactions with some transaction IDs and Employee Numbers. I have two other tables which are basically just a column full of transactions or employees that need to be filtered out

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.