Please help me understand what can and can't be added to strings:

Consider the following:

  char arrayOne [100] {"First array."};
  char arrayTwo [100] {"Second array."};
  char oneA {'a'};
  char oneB {'b'};
  int one {1};
  int two {2}; 

This is allowed:

char myArray [100] {oneA};

This is not:

myArray += oneA;

Fair enough: arrays are budgeted a certain amount of memory, and we can't go over it.
I can't do this either:

char myArray [] {arrayOne + arrayTwo};

But now here's this:

char myArray [] {arrayOne + oneA};

The compiler DOES allow that, with an invalid conversion from 'char*' to 'char' warning. (arduino's Serial.print() just results in a single mirrored question mark, though, so clearly something is up). Nevertheless: here the compiler does NOT object to adding beyond an array's length, at least for the sake of copying, not assigning. We can even do this, with the same warning / outcome:

char myArray[100]{strcat(arrayOne + one, (strcat(arrayTwo, two)))};

Meanwhile, this doesn't even give a warning:

String myString {arrayOne + oneA};

It doesn't yield anything when printed with Serial.print(), though.

So, I'm left very confused. I can't see any sense in when the + operator can and can't be used with C strings. Surely there must be some warning-free way of combining strings, at least for assignment, beyond just using loops to go through all the spots in the arrays.

Any insight into the rationale here would be appreciated.

is not the same as:

String myString constructs an instance of the String class and that object has methods and operator overrides associated with it. For example, the '+' operator is overridden to implement adding characters to the string.

char myArray [] defines an array of characters which are generically referred to as "strings" but not the same as the class String. It is just like any other array but it is characters rather than, say, int. In other words it defines an array of the data type char. If you want to add, concatenate, search, copy, etc. you can use C++ library functions like strcat, etc. Here is a summary of them:

C standard library string.h functions

You are looking for

  strcpy(array,"message");
  strcat(array,"message2");

Do yourself a favor and do NOT use Strings. (Note the capital S)

a few corrections to your sample code in post #1 plus an examples of C string concatenation and C++ std::string class

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main(void) {
    // C char arrays
    char arrayOne [100] ={"First array."};
    char arrayTwo [100]= {"Second array."};
    char oneA= {'a'};
    char oneB ={'b'};
    int one ={1};
    int two ={2};
    // concatenate C arrays
    char arrayThree [100]= {oneA};
    strcat(arrayThree, arrayOne);
    strcat(arrayThree, arrayTwo);
    cout << arrayThree << endl;
    // C++ string class
    string arrayFour="fourth array ";
    string arrayFive ="fifth array";
    // concatenate C++ strins
    string arraySix=arrayFour+arrayFive;
    cout << arraySix;
  }

a run gives

aFirst array.Second array.
fourth array fifth array

note the above is using the C++ std::string class not the Arduino String String class

If you are wondering why to not use the String class take a look here.

Basically, it can't. "+" is not needed because there exists an entire collection of very simple functions to manipulate C-strings.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.