C语言 Arduino 字符串、字符和指针等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14436791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:06:04  来源:igfitidea点击:

Arduino strings and chars and pointers and such

carrayschararduino

提问by ilium007

I am finding myself again confused by C strings, chars, etc.

我发现自己再次被 C 字符串、字符等弄糊涂了。

Here is some code I am using to test the syntax on the Arduino. I know that (*message)buff will give me a pointer (I still don't really know why I need to use pointers, but I can do some research on that!), I convert the *message_buff to a String (just for something to do, but note that later on when I try and print this string to serial I only get a single 'c' character).

这是我用来测试 Arduino 语法的一些代码。我知道 (*message)buff 会给我一个指针(我仍然不知道为什么我需要使用指针,但我可以对此做一些研究!),我将 *message_buff 转换为字符串(仅用于有些事情要做,但请注意,稍后当我尝试将此字符串打印为串行时,我只会得到一个“c”字符)。

I set an array pointer three elements long (three bytes long?? I don't really know):

我设置了一个数组指针三个元素长(三个字节长??我真的不知道):

char *mqtt_command[3] = {};

And later on when I try and add a value to the array using:

稍后当我尝试使用以下方法向数组添加值时:

*mqtt_command[i] = str;

I get the error:

我收到错误:

error: invalid conversion from 'char*' to 'char'

错误:从“char*”到“char”的无效转换

If I change that to:

如果我将其更改为:

mqtt_command[i] = str;

(without the *) it compiles fine. I don't know why...

(没有 *)它编译得很好。我不知道为什么...

Here is my code:

这是我的代码:

char *message_buff = "command:range:1";
char *str;
String msgString = String(*message_buff);
char *mqtt_command[3] = {};
int i = 0;

void setup()
{
  Serial.begin(9600);
  delay(500);

  while ((str = strtok_r(message_buff, ":", &message_buff)) != NULL)
  {
    Serial.println(str);
    mqtt_command[i] = str;
    i++;
  }

  delay(1000);

  Serial.print("Command: ");
  Serial.println(mqtt_command[1]);

  Serial.print("MQTT string: ");
  Serial.println(msgString);
}

void loop()
{
  // Do something here later
}

And here is the output:

这是输出:

command
range
1
Command: range
MQTT string: c

How can I understand chars, strings, pointers, and char arrays? Where can I go for a good all round tutorial on the topic?

如何理解字符、字符串、指针和字符数组?我在哪里可以找到有关该主题的全面教程?

I am passing in a command string (I think it is a string, maybe it is a char array????) via MQTT, and the message is:

我正在通过 MQTT 传递一个命令字符串(我认为它是一个字符串,也许它是一个字符数组????),消息是:

command:range:1

I am trying to build a little protocol to do things on the Arduino when an MQTT message is received. I can handle the MQTT callbacks fine, that not the problem. The issue is that I don't really understand C strings and chars. I would like to be able to handle commands like:

我正在尝试构建一个小协议,以便在收到 MQTT 消息时在 Arduino 上执行操作。我可以很好地处理 MQTT 回调,这不是问题。问题是我不太了解 C 字符串和字符。我希望能够处理如下命令:

command:range:0
command:digital:8
read:sensor:2

etc.

等等。

回答by unwind

You need a C (and/or C++) primer first, you need to work more on your understanding of the declarations and the syntax for pointer access and so on.

您首先需要 C(和/或 C++)入门,您需要更多地了解声明和指针访问的语法等。

This:

这个:

char *mqtt_command[3] = {};

means "mqtt_commandis an array of 3 char *", i.e. three pointers to characters. Since strings are represented as pointers to characters, this can be called "an array of three strings". There's no actual spacefor the characters themselves though, so this is not enough to work with but it's a good start.

表示“mqtt_command是一个由 3 组成的数组char *”,即三个指向字符的指针。由于字符串表示为指向字符的指针,因此可以将其称为“三个字符串的数组”。虽然角色本身没有实际空间,所以这还不够,但这是一个好的开始。

Then, your first error is this code:

然后,您的第一个错误是以下代码:

*mqtt_command[i] = str;

The problem the compiler is complaining about is that you're dereferencing things too many times. Just mqtt_command[i]is enough, that evaluates to the i:th value of the array, which has type char *. Then, your initial asterisk dereferencesthat pointer, meaning the type of the left-hand expression is now char, i.e. it's a single character. You can't assign a pointer into a character, it (typically) won't fit.

编译器抱怨的问题是你取消引用的次数太多了。就mqtt_command[i]足够了,它的计算结果是数组的第 i:th 个值,它的类型为char *。然后,您的初始星号取消引用该指针,这意味着左侧表达式的类型现在是char,即它是单个字符。您不能将指针分配给字符,它(通常)不适合。

Drop the initial asterisk to solve this.

删除初始星号来解决这个问题。

To analyze further, this:

进一步分析,这个:

char *message_buff = "command:range:1";
String msgString = String(*message_buff);

is also wrong, for the same reason. You're dereferencing the message_buffpointer, so the argument to the String()constructor is merely the first character, i.e. c. Again, drop the initial asterisk, you mean:

也是错误的,出于同样的原因。您正在取消引用message_buff指针,因此String()构造函数的参数只是第一个字符,即c. 同样,去掉最初的星号,你的意思是:

String msgString = String(message_buf);

which can be written as just:

可以写成:

String msgString(message_buf);

回答by Krishna

mqtt_command[i] = str;

This will work, as mqtt_command[i]is already char pointer. *will redirect it to any previously allocated memory, which is not done in the code.

这将起作用,因为mqtt_command[i]已经是字符指针。*将它重定向到任何先前分配的内存,这在代码中没有完成。