-1

I have a function which is being passed a byte and I want to add the byte t the end of a character string and to pass to another function (from the MQTT library). This second function is looking for the first item to be a const char[].

void loop() {  
  byte i;
  for( i = 0; i < 48; i++) {
    buttonTest   (i);
  }
}

void buttonTest( byte buttonId ){
  char mTopic[10] = "garage/l";
  strcat(mTopic, buttonId);
  //strcpy(mTopic, buttonId);
  client.publish(mTopic, "1");
  digitalWrite(statusLEDArray[buttonId], HIGH);
}

I tried a few other things as well but can' wrap my head around getting the byte added to the end. For the above example the compiler gives me this error: invalid conversion from 'byte {aka unsigned char}' to 'const char' [-fpermissive]*

The ultimate goal is to add the buttonID to the end of the string found in mTopic, but I also need to use this to retrieve items from arrays, as shown in the last line in the sample.

EDIT Changing to the code below works, but I keep seeing suggestions that one should avoid String myVar = "My text." for memory reasons so I was trying to do so. I know I need to look at pointers more and get a better handle on them. Once I do I can probably remove the use of String, which I believe should be my ultimate goal as I have seen memory issues with sketches in the past which I believe are the result of poor String/memory management.

void loop() {  
  byte i;
  for( i = 0; i < 48; i++) {
    buttonTest   (i);
  }
}

void buttonTest( byte buttonId ){
  String mTopic[10] = "garage/l";
  mTopic = mTopic + buttonId);
  //strcat(mTopic, buttonId);
  client.publish(mTopic.c_str(), "1");
  digitalWrite(statusLEDArray[buttonId], HIGH);
}

closed as off-topic by VE7JRO, sempaiscuba, Juraj, MatsK, MichaelT Mar 8 at 17:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Arduino, within the scope defined in the help center." – VE7JRO, sempaiscuba, Juraj, MatsK, MichaelT
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    The function strcat can concatenate a zero-terminated string to another zero-terminated string: cplusplus.com/reference/cstring/strcat – Jot Mar 3 at 10:24
  • Tip: If you are working on char arrays, whose contents change, Simply use loops to concatenate two arrays. Cz these string functions need one of the arguments to be "const char *" . Its called a C string. Eg: char * name = "xxx" ; You can't change the contents of name here. – Mitu Raj Mar 3 at 16:19
1

There is a subtle distinction between an array of characters and a C string.

All C strings are arrays of characters, but not all arrays of characters are C strings.

A C string is an array of characters terminated with a NULL. Therefore C strings cannot contain NULL (zero value) characters.

There are a whole set of C library functions that operate on C strings. strcat() is one such function. It appends one C string to another. It does not append a single char to an array of chars, which is what you are trying to do.

It looks like you have an array of chars, not a C string. (In your for loop, you try to append a 0 {a NULL} onto a C string with strcat(), which will not work.) So you should stop using string functions like strcat() and just index into your array of chars directly.

  • 1
    Nitpicking guy here: the zero character is called ASCII “NUL”, whereas “NULL” usually means a NULL pointer. – Edgar Bonet Mar 3 at 19:55
  • I meant a null byte. Is it an ASCII NUL in a C string, or a NULL value? Seems to be C strings treat the null as an "out of band" non-ASCII value, which would justify my spelling. I guess you could also argue that it is an ASCII NUL. – Duncan C Mar 3 at 19:59
  • The ASCII NUL has the numeric value zero, so yes, that's what terminates a C string. But of course, it could be argued that this terminating byte is not interpreted as ASCII. – Edgar Bonet Mar 3 at 20:02
  • Right, that's my alternate argument. The terminating byte is not ASCII, it's a null, in the same way that a null pointer is not a valid memory address. You could make the case either way. – Duncan C Mar 3 at 20:05
0

With a char array, each of its elements can be assigned a value individually.

void buttonTest( byte buttonId ){
  char mTopic[10] = "garage/l";
  mTopic[8] = (char)buttonId;
  mTopic[9] = '\0';
  client.publish(mTopic, "1");
  digitalWrite(statusLEDArray[buttonId], HIGH);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.