too many initializers for 'char [10]'

Hi all,

I have a problem with this array,

char speeds[10] = {"slow", "fast"};

If I change the number inside the in 2 or 3 or any other number, nothing changes.

With this variabile I must write in serial monitor, "slow" or "fast", for change speed of the motor, if I test this with a variable,

char slow = 's';

So it's work.

But, the problem is the IDE says "Too many initializers for char[10]", but it's not true.

You can only have one string there. You need two char arrays for two separate strings.

mirco9779x:
Hi all,

I have a problem with this array,

char speeds[10] = {"slow", "fast"};

Looks like you want to declare two char arrays of length 5 each (including terminating null char).

  char speeds[2][5] = {"slow", "fast"};

jurs:
Looks like you want to declare two char arrays of length 5 each (including terminating null char).

  char speeds[2][5] = {"slow", "fast"};

Ok, I solved, but now I have another problem, when I call the Array,

char command; (Out of Loop function)
char speeds[2][5] = {"slow", "fast"}; (Out of Loop function)

if (command == speeds[0]){ (Inside Loop function)

analogWrite(motor, 100);

}

IDE says me, "ISO C++ forbids comparison between pointer and integer [-fpermissive]".

char speeds[9] = {"slow" "fast"};
if (command == speeds[0]){    (Inside Loop function)

That isn’t how you compare strings. Google the strcmp function.

IDE says me, "ISO C++ forbids comparison between pointer and integer [-fpermissive]".

That's correct, it does.

          if (command == speeds[0]){    (Inside Loop function)

command is an integer, speeds[0] is an array (aka pointer).

It makes no sense to compare a single letter to four letters.

Look at capturing a whole string, and then using strcmp

You can't compare a string like that. It's because it's an array of variables and you can only compare one variable at a time with ==. Use strcmp() instead.

Did a post disappear?

Ok, I solved, I add "[5]", after "[0]" and now it's work. Thanks to all.

if (command == speeds[0][5]){
analogWrite(motor, 100);
}

Delta_G:
Did a post disappear?

Yes, it did, but it is back now.

mirco9779x:
Ok, I solved, I add "[5]", after "[0]" and now it's work. Thanks to all.

if (command == speeds[0][5]){
analogWrite(motor, 100);
}

No, that's not a solution, but it is a good demonstration of why you should use code tags.

AWOL:
No, that’s not a solution, but it is a good demonstration of why you should use code tags.

Why thats not a solution?

Because like a lot of people say, that's not how you compare a string. I may compile but it certainly doesn't work :wink:

mirco9779x:
Ok, I solved, but now I have another problem, when I call the Array,

char command; (Out of Loop function)
char speeds[2][5] = {"slow", "fast"}; (Out of Loop function)

if (command == speeds[0]){ (Inside Loop function)

analogWrite(motor, 100);

}

IDE says me, "ISO C++ forbids comparison between pointer and integer [-fpermissive]".

Looks like you want 'command' to be a string, too.

Perhaps:

char command[5];

But why do you want to use strings for all variables? Atmega controllers have very little RAM and each string will use some amount. It's nonsense to waste RAM for huge amounts of strings in a program.

What about using 'speaking constant names' with an enumeration (enum), you can assign to a variable?

  enum {SLOW, FAST}; // create some 'speaking constant names', SLOW=0, FAST=1
  char speedStr[2][5] = {"slow", "fast"}; 
  byte command=SLOW;
  Serial.println(speedStr[command]);
  if (command==SLOW) analogWrite(motor, 100);
  else analogWrite(motor, 500);

BTW: If you really want to deal with different strings (nullterminated strings) and compare them, the comparison is NOT done with numeric operator "==", but by calling the 'strcmp()' function.

jurs:
Looks like you want 'command' to be a string, too.

Perhaps:

char command[5];

But why do you want to use strings for all variables? Atmega controllers have very little RAM and each string will use some amount. It's nonsense to waste RAM for huge amounts of strings in a program.

What about using 'speaking constant names' with an enumeration (enum), you can assign to a variable?

  enum {SLOW, FAST}; // create some 'speaking constant names', SLOW=0, FAST=1

char speedStr[2][5] = {"slow", "fast"};
  byte command=SLOW;
  Serial.println(speedStr[command]);
  if (command==SLOW) analogWrite(motor, 100);
  else analogWrite(motor, 500);




BTW: If you really want to deal with different strings (nullterminated strings) and compare them, the comparison is NOT done with numeric operator "==", but by calling the 'strcmp()' function.

Ok, I'm a beginner in C++ and I don't know what is "enum" and "byte" variables (if is variables).

 if (command == speeds[0][5])

So you only want to check to see if command is equal to a null character? (speeds[0][5] is the null terminator on the end of "slow") This would be easier in that case:

 if (command == 0)

Delta_G:
speeds[0][5] is the null terminator on the end of "slow")

No, 5 is an overshoot of the array :wink: So it's probably the first entery of the next part. So speeds[1][0]

septillion:
No, 5 is an overshoot of the array :wink: So it’s probably the first entery of the next part. So speeds[1][0]

You are correct sir. My bad.

septillion:
No, 5 is an overshoot of the array :wink: So it's probably the first entery of the next part. So speeds[1][0]

But the [ 1 ] and [ 0 ], what is?

1 is the second object of the array when I call them?

Is the first ([ 1 ]) or second ([ 0 ]) for call objects ?