Changing an element in a char array

I am having some difficulty trying to get this right - I want to change the first character of each element in the code_Array.

I know this is not correct as it does not compile, any help would be appreciated.

// test routine to change the first letter of the code_Array
char *controller;
char code_Array[][7] = {"?CAB1z", "?CAB2z", "?F001z", "?R001z", "?S001z", "?S002z", "?DS00z"};



void setup() {


}

void loop() {



  for (int x = 0; x < 6; x++) {

    char Temp[7];
    char Train[] = "B";
    char Code[4];
    char result[7];

    strcpy(Temp, code_Array[x]);
    strcat(Code, code_Array[x][1, 5]);

    strcpy(result, Train);
    strcat(result, Code);

    strcpy(controller, result);

    Serial.println(controller);

  }


}

Please copy and paste the entire error message in a new post.

Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\ann\Downloads\Documents\Arduino\A-2022-ann\tester\tester.ino: In function 'void loop()':

tester:20:10: error: initializer fails to determine size of 'Train'

 char Train[] = 'B';

      ^~~~~

tester:20:20: error: array must be initialized with a brace-enclosed initializer

 char Train[] = 'B';

                ^~~

C:\Users\ann\Downloads\Documents\Arduino\A-2022-ann\tester\tester.ino:25:36: warning: invalid conversion from 'char' to 'const char*' [-fpermissive]

 strcpy(Code, code_Array[x][1, 5]);

              ~~~~~~~~~~~~~~~~~~^

In file included from C:\Users\ann\Downloads\Documents\ArduinoData\packages\arduino\hardware\avr\1.8.5\cores\arduino/Arduino.h:25:0,

             from sketch\tester.ino.cpp:1:

c:\users\ann\downloads\documents\arduinodata\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\string.h:305:14: note: initializing argument 2 of 'char* strcpy(char*, const char*)'

extern char *strcpy(char *, const char *);

          ^~~~~~

exit status 1

initializer fails to determine size of 'Train'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

char Train[] = "B"; //for a C-string
 --or--
char Train = 'B'; //for a single character

Oh sure. You are trying to initialize an array of characters (i.e. a character string) with a char constant (a single character). They are not the same thing, so you get an error.

strcpy(Code, code_Array[x][1, 5]);

This is just nonsense. You can't just make up C syntax. An array index can not be a list. C doesn't even have proper lists... at least not this kind. C++ STL has List objects but that is different.

As a side note, you use a lot of hard coded "magic numbers". That is going to kill you soon, if not already.

Did you go back and correct your code? Please, no! Post updates in a new message.

The elements of your array are characters. So why not just assign the first letter of each word the character you want?

for (int x = 0; x < 7; x++) {
   code_Array[x][0] = 'B';
  }

Thank you - that will work for me.

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