Thread: enum constants as array indices

  1. #1
    Registered User
    Join Date
    Jul 2005

    enum constants as array indices

    Someone said it is dangerous to use enum constants as array indices. Why?
    Is it dangerous because some compilers do not support the arithmetics of enum constants, like in the case of A[MON+TUE] given enum DAYS{MON, TUE, ...}?
    And is there anything wrong in the following?
    enum DAYS {MON, TUE, WED, totaldays};
    char *days[] = {"Monday", "Tuesday", "Wednesday"};
    for (i=0; i<totaldays; i++)
       if (strcmp(string, days[i])==1) ...
    Any possible improvement?
    And from the information-hiding perspective, let's say I do not want the client of the above code know about MON, TUE, etc, i.e. the enum declaration. They need know only about "Monday", "Tuesday", etc. So, I put only the days[] declaration in a header file (i.e. an interface). Is it a good idea? It seems the enum declaration and the days[] declaration are so tightly coupled that they should be together. ???
    Thank you.
    Last edited by hzmonte; 01-23-2006 at 07:33 PM.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Sydney, Australia
    I'm not sure what you're trying to achieve with the above code.

    To me, the only thing the enum buys you is a convenient "totaldays" value that you've used to terminate your for loop, but this doesn't buy you much. There are other better ways to determine the number of elements in an array, without duplicating the data.

    In the above, you have to change two things to update one piece of data. For example, if you added "Thursday", you'd have to add it both to the days array, and the DAYS enum. What if you (or someone else maintaining your code) forgets the other one?

    The days of the week are already well indexed (you can use the numbers 0 to 6), so you don't really need a redundant ENUM to make things any clearer.

    char *days[] = {"Monday", "Tuesday", "Wednesday"};
    totaldays = sizeof days / sizeof (char *);
    for (i=0; i<totaldays; i++)
       if (strcmp(string, days[i])==1) ...
    Last edited by cwr; 01-23-2006 at 07:46 PM.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertical Scroller laser cannon problem
    By Swarvy in forum Game Programming
    Replies: 5
    Last Post: 05-02-2009, 06:30 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM