Invalid types 'char[int]' for array subscript

I am trying to set all segments of a char array according to the letters of a string.
My code is:

/*

*/
String str ;
int strLenght;

void setup() {

}

void loop() {
   str = "A String";
   strLenght = str.length();
   char charectersInstr = new char[strLenght];
  for (int i =  0; i <= strLenght; i++){
    charectersInstr[i]=str.charAt(i);
  }
  }

I get the error:

invalid types 'char[int]' for array subscript```

Please post a small but complete sketch that illustrates the problem

Hint: new returns a pointer

why not simply

char [LEDLightLenght];

I get the following error:
error: expected primary-expression before 'char'

We can't see your code.

I am trying to set all segments of a char array according to the letters of a string.
My code is:

/*

*/
String str ;
int strLenght;

void setup() {

}

void loop() {
   str = "A String";
   strLenght = str.length();
   char charectersInstr = new char[strLenght];
  for (int i =  0; i <= strLenght; i++){
    charectersInstr[i]=str.charAt(i);
  }
  }

I get the error:

invalid types 'char[int]' for array subscript```

Try:

  char charectersInstr[strLenght];

charectersInstr is a single character; you will need a pointer to a character:

char *charectersInstr = new char[strLenght];

Dynamic memory allocation has to be done with care (and probably not at all). You will need to free/delete the memory before you leave loop(). Your current (attempted) code will run out of memory.

The below by @wildbill will be a lot safer.

@nousernameavailable

Other post/duplicate merged with this one
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

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