I am a bit stuck on how to create an array so i can create a string to declare a colour.

I have the following code:

char[7] color; //biggest color: yellow (6) + \o (1) = 7

for(int i = 0; i < ROWS; i++) //5 rows
        case 0:     color = "RED";
        case 1:     color = "ORANGE";
        case 2:     color = "YELLOW";
        case 3:     color = "BLUE";
        case 4:     color = "GREEN";
        default:    color = "BLACK";

But i get this error:

 error: expected identifier or '('

Where am i going wrong here - and is there any other mistake I have made?

1 Answer 1


If you know the color names, why not simply create an array of strings to hold them?

char* color[] = {"RED","ORANGE","YELLOW","BLUE","GREEN","BLACK"};

Then, wherever you need to assign a color, you could just say

newcolor = color[2]; //sets the string 'newcolor' to "YELLOW"

Or if you are using that color in a function that takes a string:

setColor(thing,color[4]);  //sets the color of thing to "GREEN"


  • I thought you had to define its size before hand ?
    – user7869
    Aug 4, 2015 at 0:08
  • by initialising the array at the time of declaration, you are defining its size.
    – curiouskiwi
    Aug 4, 2015 at 0:27
  • Kiwi is right, but just out of curiosity, which line does the error say is causing this problem? Usually, that error is a structural issue, like misplaced braces, semicolons, or the like. My bet is that it was char[7] color;
    – Cliff B
    Aug 4, 2015 at 0:33
  • @CliffB i can't remember now - i removed the case statement and used a pre declared array which shortened the length of my script.
    – user7869
    Aug 4, 2015 at 0:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .