使用strtok_r函数失败
请帮我修复此代码。我现在迷失了 strtok 功能。我收到关于“if (tokens[0] == "A")”行的消息“ISO C++ 禁止指针和整数之间的比较”
if (started && ended)
{
char *p = inData;
char *tokens[50];
int i = 0;
while (i < 50) {
tokens[i] = strtok_r(p,",",&p);
if (tokens[i] == NULL) {
break;
}
i++;
}
if (tokens[0] == 'A'){
pinMode(atoi(tokens[1]),OUTPUT);
analogWrite(atoi(tokens[1]),atoi(tokens[2]));
}
else if (tokens[0] == 'D')
{
if (atoi(tokens[2]) == 1)
{
pinMode(atoi(tokens[1]),OUTPUT);
digitalWrite(atoi(tokens[1]),HIGH);
}
else if (atoi(tokens[2]) == 0)
{
pinMode (atoi(tokens[1]),OUTPUT);
digitalWrite(atoi(tokens[1]),LOW);
}
}
started = false;
ended = false;
index = 0;
}
Please help me fix the this code. I am lost with strtok function now. I get message "ISO C++ forbids comparison between pointer and integer" about the line "if (tokens[0] == "A")"
if (started && ended)
{
char *p = inData;
char *tokens[50];
int i = 0;
while (i < 50) {
tokens[i] = strtok_r(p,",",&p);
if (tokens[i] == NULL) {
break;
}
i++;
}
if (tokens[0] == 'A'){
pinMode(atoi(tokens[1]),OUTPUT);
analogWrite(atoi(tokens[1]),atoi(tokens[2]));
}
else if (tokens[0] == 'D')
{
if (atoi(tokens[2]) == 1)
{
pinMode(atoi(tokens[1]),OUTPUT);
digitalWrite(atoi(tokens[1]),HIGH);
}
else if (atoi(tokens[2]) == 0)
{
pinMode (atoi(tokens[1]),OUTPUT);
digitalWrite(atoi(tokens[1]),LOW);
}
}
started = false;
ended = false;
index = 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
if (tokens[0] == 'A')
行中,字符常量 'A' 只是 A - 65 或 0x41 的 ASCII 值,而tokens[0]< /code> 是一个
char *
。因此,您有“指针和整数之间的比较”。你到底想做什么?要检查令牌的第一个字符是否是大写 A,请编写
要检查它是否是字符串“A”,请编写
另外,您对
strtok_r
的使用是错误的。您正在使用输入缓冲区来存储上下文信息,因此会覆盖它。 (您真的需要使用可重入版本吗?标准strtok
不需要上下文存储。)它应该看起来像此外,您最好使用
strsep
> 代替strtok
像这样In the line
if (tokens[0] == 'A')
, the character constant 'A' is simply the ASCII value of A - 65 or 0x41, whiletokens[0]
is achar *
. Hence you have a "comparison between pointer and integer".What is it that you mean to do? To check whether the first character of the token is a capital A, write
To check whether it is the string "A", write
Also, your use of
strtok_r
is wrong. You are using the input buffer to store context information and so overwriting it. (Is there really a reason you need to use the reentrant version? The standardstrtok
doesn't require context storage.) It should look likeIn addition you are better off using
strsep
in place ofstrtok
like this好吧,那行(我在你的代码中找不到)确实是错误的。
tokens[0]
是char*
,'A'
是char
。如果要比较所有字符串,请使用strcmp
。如果您想检查tokens[0]
的第一个字符,请使用tokens[0][0]
。Well, that line (which I couldn't find in your code) is really wrong.
tokens[0]
is achar*
, and'A'
ischar
. If you want to compare the all string, usestrcmp
. If you want to check the first char oftokens[0]
, usetokens[0][0]
.您说错误发生在该行上,
但您向我们展示的代码中没有这样的行。
我假设您实际上的意思如下:
上面不起作用的原因是
tokens[0]
是char*
类型,并且您无法比较 < code>char* 到这样的char
。如果要比较两个字符串是否相等,则需要进行字符串比较:You say that the error occurs on the line
yet there is no such line in the code that you show us.
I assume you actually meant the following:
The reason the above won't work is that
tokens[0]
is of typechar*
, and you can't compare achar*
to achar
like this. You need a string comparison if you want to compare the two strings for equality: