帮助理解 c++ 中的 const char* 运算符
我需要将一些代码从 c++ 转换为 java,并且我不太确定如何处理“const char*”运算符。检查下面的代码:
const char* handText;
const char* index = strchr(handText, '-');
char handCeil[4];
char handFloor[4];
strncpy(handCeil, handText, index - handText);
strcpy(handFloor, index + 1);
我得到的是这样的:
String handText;
String index = handText.substring(handText.indexOf('-'));
char handCeil[4];
char handFloor[4];
但是,我不知道当你在 c++ 中添加整数 (index + 1) 或减去 (index - handText) 字符串时这意味着什么。抱歉,如果这是一个愚蠢的问题,但我从未学过如何用 C++ 编程,只学过 Java。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这
相当于
所以它用“-”分割字符串并将第一部分(我认为包括“-”本身)放入
handCeil
中,将其余部分放入handFloor
>。index - handText
表示:index
指向特定字符,handText
指向该字符串的开头。如果将两者相减,则会得到这两个指针之间的字符数,或者第一个“-”的数组索引。strncpy
复制n
个字节,因此如果index
指向第 3 个字符,它将复制 3 个字符。index + 1
表示指向index
指向的字符1之后的字符。This
is equivalent to
So it splits the string by the '-' and puts the first part (including the '-' itself, I think) into
handCeil
and the remainder intohandFloor
.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
copiesn
bytes, so ifindex
points to the 3rd character it will copy 3 characters.index + 1
means point to the character 1 after the one pointed to byindex
.在 Java 中你不能乱用指针。您可以使用
String
方法,例如subString(int startIdx, int endIdx)
提取子字符串以分配给 handCeil 和 handFloor。我更喜欢将它们保留为 JavaString
。如果稍后您需要访问单个字符,您仍然可以通过 charAt(int idx) 方法来完成。You cannot muck around with pointers in Java. Either you use
String
methods such assubString(int startIdx, int endIdx)
to extract out substring to assign to handCeil and handFloor. I would prefer keeping them as JavaString
. If later you need to access the individual characters you could do it anyways throughcharAt(int idx)
method.向 char*(指针)加 1 会使指针增加一个字符。
因此,在提供的代码中,由于索引指向 handText 中的“-”,因此递增它将导致指针现在指向下一个字符。
(顺便说一句,提供的 C++ 代码一点也不安全,并且会导致 handText 的许多可能值出现严重错误,例如“这是一个字符串-”。;)
Adding one to a char* (pointer) increments the pointer one 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.
(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-'. ;)
该代码片段调用未定义的行为。
handText
是一个未初始化的指针常量。将其作为参数传递给strchr
会调用 UB。strchr
返回指向handText
中第一次出现-
的指针,但handText
没有指向任何位置或垃圾。The snippet invokes undefined behavior.
handText
is an uninitialized pointer constant. Passing it as a parameter tostrchr
invokes UB.strchr
returns the pointer to the first occurence of-
inhandText
buthandText
is pointing no where or garbage.