简体   繁体   English

帮助理解C ++中的const char *运算符

[英]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. 我需要将一些代码从c ++转换为java,但我不确定如何处理'const char *'运算符。 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++. 但是,我不知道在c ++中添加整数(index + 1)或减去(index-handText)字符串时的含义。 Sorry if this is a stupid question, but I never learned how to program in c++, only in java. 抱歉,如果这是一个愚蠢的问题,但我从未学习过仅使用Java进行C ++编程的方法。 Thank you. 谢谢。

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 . 因此,它将字符串用'-'分割,然后将第一部分(我认为包括'-'本身)放入handCeil ,其余部分放入handFloor

index - handText means this: index points to a specific character, handText points to the beginning of that string. index - handText意思是: index指向特定字符, handText指向该字符串的开头。 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. strncpy复制n个字节,因此,如果index指向第3个字符,它将复制3个字符。 index + 1 means point to the character 1 after the one pointed to by index . index + 1装置指向字符1之后,通过一个指向index

You cannot muck around with pointers in Java. 您不能在Java中随意使用指针。 Either you use String methods such as subString(int startIdx, int endIdx) to extract out substring to assign to handCeil and handFloor. 您可以使用诸如subString(int startIdx, int endIdx)类的String方法提取出子字符串以分配给handCeil和handFloor。 I would prefer keeping them as Java String . 我宁愿将它们保留为Java String If later you need to access the individual characters you could do it anyways through charAt(int idx) method. 如果以后需要访问各个字符,则可以通过charAt(int idx)方法进行操作。

Adding one to a char* (pointer) increments the pointer one char. 将一个加到char *(指针)可以使指针增加一个char。

So in the code provided, since index points to the '-' in handText, incrementing it will cause the pointer to now point to the next character. 因此,在提供的代码中,由于索引指向handText中的“-”,因此递增索引将导致指针现在指向下一个字符。

(BTW, the C++ code provided isn't at all secure and will cause significant errors for many possible values of handText, like 'this is a string-'. ;) (顺便说一句,提供的C ++代码一点也不安全,并且会为handText的许多可能值(例如'this is a string-')造成重大错误。)

The snippet invokes undefined behavior . 该片段调用未定义的行为

const char* handText;
const char* index = strchr(handText, '-');

handText is an uninitialized pointer constant. handText是未初始化的指针常量。 Passing it as a parameter to strchr invokes UB. 将其作为参数传递给strchr调用UB。 strchr returns the pointer to the first occurence of - in handText but handText is pointing no where or garbage. strchr将指针指向在handText第一次出现的- ,但是handText没有指向任何位置或垃圾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM