Using string to reference Int array in function

Working on my first Arduino project using NeoPixels. I'm fairly familiar with programming (PHP, JS, node) but C is new to me and stuck at this point.

Basic gist is creating themes (palettes of colours) that can be selected by the user.

I have the colours defined as an INT array with RGB values e.g.

INT red[3] = {255,0,0}; // r, g, b
INT blue[3] = {0,0,255}
etc.

These are then stored in themes e.g.

char* rainbow[] = {"red", "orange", "yellow" ... };

Later, I call these in a loop:

rainbow[1] returns string "orange" etc as expected. All this section works.

However, I need to then pass the color name onto a function:

void colorStrip(int color[3]) { ... // do some colour stuff

The function is expecting an array such a one of the colors defined above e.g. colorStrip( red )...and this works fine.

However, I want to be able to use the string returned from rainbow[1] e.g

colorStrip( rainbow[1] );

What's the best way to accomplish this?

What's the best way to accomplish this?

Start with realistic expectations. The compiler strips out all names, and replaces them with addresses. orange is not an address.

You need some if statements to compare the name to the name that the compiler substitutes with an address, to select the address to use.

   if(strcmp(rainbow[n], "orange") == 0) // If they match
      colorStrip(orange);
   else // the rest of the if statements go here.

Of course, there are ways, using structs, for instance, to relate a name and a series of values. Once you know the name, you can find the struct, and get the values.

Paul -

Thanks for your answer. I ended up going in this general direction based on your approach and everything is working now. The only drawback here is with lots of colours adding the code for each can be tedious but for my purposes it was perfect.

Cheers-

If you're intent on using this string-based approach, the preprocessor's stringify operator can take a lot of the legwork away, and make your code easier to maintain.