Declare Char Array

Hello,

I am having a problem with declare a char array.

char A11[]= {"Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5"};

I dont know which is my mistake.

Ty,

If you're trying to put multiple strings then it needs to be a 2 dimensional array.

Delta_G: If you're trying to put multiple strings then it needs to be a 2 dimensional array.

I try to put this.

char A11[]= {"Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5"}; char A12[]= {"Libro 6", "Libro 7", "Libro 8", "Libro 9", "Libro 10"};

But I have an error in the first line i dont know

If you want to put multiple strings in a char array then it has to be a 2 dimensional array. You've created 2 1 dimensional arrays now and are trying to put multiple strings in each of them. You've taken one error and now are making it twice.

Go google "C++ 2 dimensional array" if you don't know what that is.

You need either a 2D array or an array of strings.

char *s = "This is a string";

char *ss[10]; // This is an array of strings.

It doesnt work :(

Somebody can write an example plz?

Ty

KTFLocke: It doesnt work :(

Somebody can write an example plz?

Ty

What doesn't work? What did you write? What Paul wrote would work. Declaring it as a 2D array would work. Post what you wrote that didn't work.

Delta_G: What doesn't work? What did you write? What Paul wrote would work. Declaring it as a 2D array would work. Post what you wrote that didn't work.

char **A11[][]= {"Libro 1", "Libro 2"}, "Libro 3"};

"declaration of A11 as multidimensional array must have bounds for all dimension excepts the first".

Well it told you exactly the problem. You must have bounds for the first dimension. Have you tried doing that?

char A11[8][] = {"Libro 1" , "Libro 2" , "Libro 3"};

I used 8 because your strings are all 7 characters long and you need one extra for the terminating null at the end of each one.

I used the following in some code.

char *channelName[] = {"Throttle", "Aileron", "Elevator", "Rudder", "Gear", "Aux"};

I could index the names with:

Serial.print(channelName[i]);

I was a bit surprised it worked but it did.

Isn't this what the OP is trying to do?

It looks like the original code was very close to doing what was intended. I think this would work:

char *A11[]= {"Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5"};

DuaneDegn: I used the following in some code.

char *channelName[] = {"Throttle", "Aileron", "Elevator", "Rudder", "Gear", "Aux"};

I could index the names with:

Serial.print(channelName[i]);

I was a bit surprised it worked but it did.

Isn't this what the OP is trying to do?

Yes, that it what @PaulS suggested. Either way works but I tend to goto the 2-D char array one because I mostly put my strings in PROGMEM and you can't do that with an array of pointers.

Delta_G: Well it told you exactly the problem. You must have bounds for the first dimension. Have you tried doing that?

char A11[8][] = {"Libro 1" , "Libro 2" , "Libro 3"};

I used 8 because your strings are all 7 characters long and you need one extra for the terminating null at the end of each one.

Yes, I have but it appears the same problem with bounds.

KTFLocke: Yes, I have but it appears the same problem with bounds.

What problem. Post the code you used and the error message you received. Post the WHOLE code, not just one or two lines this time. And put it in code tags this time.

char *LibrosA[] = {"Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5"};
 Serial.print(LibrosA[i]);




void setup(){
  Serial.begin(9600);
}


void loop(){
}

I changed "A11" for "LibrosA" because i have some problems with that variable.

Error: expected constructor, destructor, or type conversion before "." token

You of course don't use a serial call outside a function. You use an undeclared variable "i".

This compiles:

char *LibrosA[] = {"Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5"};
void setup(){
  Serial.begin(9600);
  Serial.print(LibrosA[0]);
}
void loop(){
}

DuaneDegn: You of course don't use a serial call outside a function. You use an undeclared variable "i".

This compiles:

char *LibrosA[] = {"Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5"};
void setup(){
  Serial.begin(9600);
  Serial.print(LibrosA[0]);
}
void loop(){
}

DuaneDegn: You of course don't use a serial call outside a function. You use an undeclared variable "i".

This compiles:

char *LibrosA[] = {"Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5"};
void setup(){
  Serial.begin(9600);
  Serial.print(LibrosA[0]);
}
void loop(){
}

It works ! Ty ! But now, How can I see in Monitor Serial Libro 1 Libro 2 bla bla??

char *LibrosA[] = {"Libro 1", "Libro 2", "Libro 3", "Libro 4", "Libro 5"};
const byte NAMES_IN_USE = 5;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  for (byte i = 0; i < NAMES_IN_USE; i++)
  {
    Serial.println(LibrosA[i]);
  }

  while (1) {} // endless loop to stop program
}

The above code will output the following:

Libro 1
Libro 2
Libro 3
Libro 4
Libro 5