Arduino strings, symbols and pointers, etc.

I am again confused again by C strings, characters, etc.

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

I am setting a pointer to an array 3 elements long (3 bytes long, I really don't know):

char *mqtt_command[3] = {};

      

and then when I try to add a value to the array using:

*mqtt_command[i] = str;

      

I am getting the error:

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

      

if i change this to:

mqtt_command[i] = str;

      

(without *) it compiles fine. I do not 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 result:

command
range
1
Command: range
MQTT string: c

      

Can anyone help me understand characters, strings, pointers, char arrays? Where can I find a good tutorial on this topic?

What I am doing is pass on the command line (I think it is a string, maybe it is a char ???? array) via MQTT, the message is:

command:range:1

      

I am trying to create a small protocol to do something on Ardiuno when an MQTT message is received. I can handle MQTT callbacks just fine, this is not a problem, the problem is that I don't really understand C strings and characters. I would like to be able to handle commands like:

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

      

etc.

Any help would be greatly appreciated.

+3


source to share


2 answers


You first need a C (and / or C ++) primer, you need to work harder on understanding declarations and syntax for pointer access, etc.

It:

char *mqtt_command[3] = {};

      

means that " mqtt_command

is 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 place for the characters themselves, so it's not enough to work with, but it's a good start.

Then your first mistake is the code:

*mqtt_command[i] = str;

      

The problem the compiler is complaining about is that you are enacting too many times. Just mqtt_command[i]

enough that it evaluates the i: th value of an array of type char *

. Then your initial star markings, which is a pointer, i.e., the type of the left expression, now char

, i.e. This is one character. You can't assign a pointer to a character, it (usually) doesn't match.



Dropping the initial asterisk to fix this problem.

For further analysis:

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

      

also erroneous for the same reason. You are looking for a pointer message_buff

, so the constructor argument String()

is just the first character, i.e. c

... Again, drop the leading asterisk, you mean:

String msgString = String(message_buf);

      

which can be written as simple:

String msgString(message_buf);

      

+7


source


mqtt_command[i] = str;

      



This will work since it is mqtt_command[i]

already a character pointer. *

will redirect it to any previously allocated memory, which is not done in the code.

0


source







All Articles