Using a loop to print an array

OK, I have been searching for the answer on the forums for over 4 hours, I hate to post a questions but I am getting frustrated!

I know this is going to be easy, at least I hope - I am just hitting a mind block. I am looking for a way to print out array values that were defined in a "Struct"

struct config_t
{
        unsigned long Val_a; // timer value a
        unsigned long Val_b; // timer value b
        unsigned long Val_c; // timer value c
        ....                       // there are a total of 16 variables here
} configuration;

I am using the "EEPROMAnything.h" method of reading and writing to the EEPROM. What I am looking to do is Serial.print a menu with the variable names (which I have loaded in a char array) along with the current values in the structure shown above.

So, I have the names located here:

char* EEVarNames[]={"","Timer A","Timer B","Timer C","... (16 var names) };  // null at beginning so first used spot is 1, not 0. Makes displaying the menu nicer :-)
Serial.println("Settings Menu");
    Serial.println("Timer Values in milliseconds.");
    Serial.println("Enter 99 to exit");
    Serial.println("------------------------------" );
      
      for (int i = 1; i < 16; i++)
      {
       Serial.print(i);   
       Serial.print(". ");
       Serial.print(EEVarNames[i]); //works great, prints out the individual names.
       Serial.print("     ");
       Serial.println(configuration.[EEVarNames[i]);  //this doesn't work... looking to walk the struct using the same technique as above.
      }

The last line there is what I am trying to figure out. How do I get it to walk the array? I have tried pointers but am not sure of the proper syntax.

I am looking for an output like this:

  1. Timer A 60000
  2. Timer B 120000
  3. Timer C 3000
    ... etc. I have a prompt at the end that asks what # you would like to change and updates the configuration.Val_x value. Once you're done you can commit it to the EEPROM. I just need to figure out how to walk the damm array! :roll_eyes:

I know, this is probably a basic question but I couldn't find any examples looking at arduino references as well as more general C++ ones.

Thanks for your help!

Steve

// there are a total of 16 variables here

Are they all unsigned long? Is there a reason configuration is not an actual array?

Or at least put an array inside the struct.

No, there are a few int's and one char for now.

Steve

I can think of two choices...

  1. 16 Serial.print calls. One for each element.

  2. "Descriptors". Essentially they are handcrafted run-time information.

configuration.[EEVarNames[i]] //this doesn't work...

Yes, this does not work.
Here, you need to have an int inside the square brackets if it's an array, or you need to use the variable name
configuration.val_A if it's a struct. The variable name is translated at compile time. You can't (easily) use a string, evaluated at run time, to find the element inside your struct.

I'd rather propose a struct config_t {char* name; long value; } and have an array of such config elements.
Perhaps you also need a type info, to know if value is really long or restricted to smaller integers.

Thanks all for your input! I guess I'll use the 16 Serial.print statements, not as elegant but it'll work and I have the memory (for now).

The idea here is to have a menu of settable variables, such as timers, frequency, name, IP address, etc., that can be accessed via the serial port, viewed, modified, and committed to the EEPROM. Maybe I could package the data differently to make this work better, but it's not hundreds of elements, it should be less than 20 or so.

One unrelated question, I am using using unsigned long for my timers as I compare them to mills(), I seem to recall that it was best practice to do it this way. I could store most of them as int's and cast them before the test against mills(). No big deal, I'd only be saving 2 bytes per in the EEPROM and there are only 10 or so unsigned long's...

Thanks again!

Steve