Go Down

Topic: Multiple array concat code advice (Read 34 times) previous topic - next topic

xlaser

C is not my favourite language, but no choice with the Arduino ide!
I'm looking to simplify the following repetitive concatation of array contents using a 'for' loop
   strcat(Myarray, Strarray1);
   strcat(Myarray, Strarray2);
   strcat(Myarray, Strarray3);
   strcat(Myarray, Strarray4);
   strcat(Myarray, Strarray5);
   strcat(Myarray, Strarray6);
   strcat(Myarray, Strarray7);
   strcat(Myarray, Strarray8);
   strcat(Myarray, Strarray9);
   strcat(Myarray, Strarray10);
   strcat(Myarray, Strarray11);


The above code does work but I feel there is a tidier method./
Any advice welcome.

luisilva

C is not my favourite language, but no choice with the Arduino ide!
(...)
Actually Arduino is programmed in C/C++ (not only raw C).

Answering the question, you can store the Strarray in a matrix and then change the row of the matrix inside an for loop.

econjack

Or you could do something like this:

Code: [Select]

void setup() {

  char *str[] = {"This is zero", "This is one", "This is two"};
  char bigStr[50];
  int i;
  int elements = sizeof(str) / sizeof(str[0]);

  Serial.begin(9600);
  bigStr[0] = '\0';        // strcat() needs to find a null

  for (i = 0; i < elements; i++) {
    strcat(bigStr, (const char*) str[i]);
    Serial.println(str[i]);
  }
  Serial.println(bigStr);
}

void loop() {

}

Go Up