使用strtok_r函数失败

发布于 2024-12-29 05:19:14 字数 789 浏览 1 评论 0原文

请帮我修复此代码。我现在迷失了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月流歌 2025-01-05 05:19:14

if (tokens[0] == 'A') 行中,字符常量 'A' 只是 A - 65 或 0x41 的 ASCII 值,而 tokens[0]< /code> 是一个 char *。因此,您有“指针和整数之间的比较”。

你到底想做什么?要检查令牌的第一个字符是否是大写 A,请编写

if (tokens[0][0] == 'A')

要检查它是否是字符串“A”,请编写

if (strcmp(tokens[0], "A") == 0)

另外,您对 strtok_r 的使用是错误的。您正在使用输入缓冲区来存储上下文信息,因此会覆盖它。 (您真的需要使用可重入版本吗?标准 strtok 不需要上下文存储。)它应该看起来像

char *token;
char *context;

for (token = strtok(p, ",", &context);
    token;
    token = strtok(NULL, ",", &context)) {

  tokens[i++] = token;
  if (i >= 50) break;
}

此外,您最好使用 strsep > 代替 strtok 像这样

char *token;
while (token = strsep(&p, ",")) {
  tokens[i++] token;
  if (i >= 50) break;
}

In the line if (tokens[0] == 'A'), the character constant 'A' is simply the ASCII value of A - 65 or 0x41, while tokens[0] is a char *. 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

if (tokens[0][0] == 'A')

To check whether it is the string "A", write

if (strcmp(tokens[0], "A") == 0)

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 standard strtok doesn't require context storage.) It should look like

char *token;
char *context;

for (token = strtok(p, ",", &context);
    token;
    token = strtok(NULL, ",", &context)) {

  tokens[i++] = token;
  if (i >= 50) break;
}

In addition you are better off using strsep in place of strtok like this

char *token;
while (token = strsep(&p, ",")) {
  tokens[i++] token;
  if (i >= 50) break;
}
乄_柒ぐ汐 2025-01-05 05:19:14

好吧,那行(我在你的代码中找不到)确实是错误的。 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 a char*, and 'A' is char. If you want to compare the all string, use strcmp. If you want to check the first char of tokens[0], use tokens[0][0].

猥琐帝 2025-01-05 05:19:14

您说错误发生在该行上,

if (tokens[0] == "A")

但您向我们展示的代码中没有这样的行。

我假设您实际上的意思如下:

if (tokens[0] == 'A')

上面不起作用的原因是 tokens[0]char* 类型,并且您无法比较 < code>char* 到这样的 char 。如果要比较两个字符串是否相等,则需要进行字符串比较:

if (strcmp(tokens[0], "A") == 0)

You say that the error occurs on the line

if (tokens[0] == "A")

yet there is no such line in the code that you show us.

I assume you actually meant the following:

if (tokens[0] == 'A')

The reason the above won't work is that tokens[0] is of type char*, and you can't compare a char* to a char like this. You need a string comparison if you want to compare the two strings for equality:

if (strcmp(tokens[0], "A") == 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文