Arduino strings and chars and pointers and such

26,030

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:

char *mqtt_command[3] = {};

means "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 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:

*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 dereferences that 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.

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_buff pointer, so the argument to the String() constructor is merely the first character, i.e. c. Again, drop the initial asterisk, you mean:

String msgString = String(message_buf);

which can be written as just:

String msgString(message_buf);
Share:
26,030
ilium007
Author by

ilium007

Updated on March 16, 2020

Comments

  • ilium007
    ilium007 about 4 years

    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.

  • ilium007
    ilium007 over 11 years
    Much appreciated - do you have any pointers (no pun intended) for a good solid C primer ?
  • Kris
    Kris over 11 years
    For quick C intro (especially arrays/string/chars) I would recommend "Head First C" amazon.com/Head-First-C-David-Griffiths/dp/1449399916/…
  • ilium007
    ilium007 over 11 years
    Ok cool - I have read other Head First books and they are very good. I will give that one a go.
  • John Bode
    John Bode over 11 years
    I would also recommend Harbison & Steele's C: A Reference Manual as a companion reference.