I am finding myself again confused by C strings, chars, etc.
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).
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’
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:
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:
command:range:0
command:digital:8
read:sensor:2
etc.
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.
This:
means “
mqtt_commandis an array of 3char *“, 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 space for the characters themselves though, so this is not enough to work with but it’s a good start.Then, your first error is this code:
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 typechar *. Then, your initial asterisk dereferences that pointer, meaning the type of the left-hand expression is nowchar, i.e. it’s a single character. You can’t assign a pointer into a character, it (typically) won’t fit.Drop the initial asterisk to solve this.
To analyze further, this:
is also wrong, for the same reason. You’re dereferencing the
message_buffpointer, so the argument to theString()constructor is merely the first character, i.e.c. Again, drop the initial asterisk, you mean:which can be written as just: