Thread: Understanding/Implementing Enumeration

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Understanding/Implementing Enumeration

    Hello, I am currently teaching myself C, and am at a point where I am learning about enumeration. Id really like some help understanding its capabilities. Here in this example I was given.

    Code:
    #include <stdio.h>
    
    enum colours {RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET,
                  NUMBER_OF_COLOURS};
    
    typedef enum colours colour_t;
    
    int main(void)
    {
       colour_t sky, forest;
    
       printf("There are %d colours in the enum\n", NUMBER_OF_COLOURS);
    
       sky = BLUE;
       forest = GREEN;
    
       printf("sky = %d\n", (int)sky);
       printf("forest = %d\n", (int)forest);
    
       return 0;
    }
    Is it possible to re work this code such that, the user could be prompted to select a number (corresponding to the colours in the enumeration), so that the output is the colour instead of the coefficient it relates to.

    Is this even possible with enumeration or are these lists just place holders for the value pertaining to it?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's no "native" way to get the enumeration name back from a value. (In fact, I don't believe the program itself is required to have the names anywhere in it -- all the names would be replaced by their values when compiling.)

    There's a couple different workarounds; you can do switches, or you could do something like
    Code:
    char *colour_names[] = {"RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "INDIGO", "VIOLET"};
    .
    .
    .
    printf("sky = &#37;s\n", colour_names[sky]);

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It is just a holders in C. I believe in other languages it is available. Though you could do something like:
    Code:
    typedef struct colourTag {
        colour_t col,
        char name[10];
    } color;
    
    color sky, forest;
    sky.col = BLUE;
    strcpy(sky.name,"blue");
    
    etc etc
    
    OR
    
    void colorName (colour_t col, color* colStruct) {
      colStruct.col = col;
       switch (col) {
       case BLUE: strcpy(colStruct.name, "blue");
                         break;                   
       etc etc
       }
    }
    
    color sky;
    colName(BLUE, *sky);
    Note that other languages just have enum as an object with built in functions. You can do the same thing in C, though you have to do everything by yourself

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Code:
    char *colour_names[] = {"RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "INDIGO", "VIOLET"};
    .
    .
    .
    printf("sky = &#37;s\n", colour_names[sky]);
    *EDIT* just working on this now.....and What I basically want to do is this, and Just tell me what the easiest way would be...

    Have a list, say...Work days, 1-sunday 2-monday...etc...

    The user inputs the coefficient: ie. 7

    The output would be : You have selected Saturday.
    Last edited by Iconate; 10-09-2008 at 02:01 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    const char* days_names[] = { "Monday",  "Tuesday", ..., "Sunday" };
    printf("&#37;s", days_names[input]);
    Btw, tabstop... tsk, tsk, you should know better
    String literals should be const char* (and not const char * either ).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There is one thing you should pay attention though. That the first value MUST start from 0. Or you need a different array. Thus, if you have sunday=1, monday=2, ...., satarday=7 then you would do:
    Code:
    const char* days_name[] = {"", "Monday", ..., "Sunday"}
    printf("%s", days_names[input]);
    OR
    const char* days_name[] = {"Monday", ..., "Sunday"}
    printf("%s", days_names[input-1]);
    if you want sunday = 0, ... then you use as Elysia wrote it.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why on Earth would anyone want the week to start on Sunday!?!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Hey! My job is open 11pm - 12am every day!


    But thanks guys, that makes sense. Follow up question though, why am I using a pointer for the array? Is it because if I called the array say WorkDay[1] and it wasn't a pointer than it would give me the address instead?
    Last edited by Iconate; 10-09-2008 at 04:32 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need an array of const char *, so that's what we've been making. (We don't want an array of just chars, since "Monday" is not a char but a const char *, i.e., a string.)

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If you don't want to do any math on-the-fly such as days_names[input-1], or waste an element on an empty string, how about
    Code:
    const char *dayn[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}, **days_names = dayn - 1;
    ...
    printf("day: &#37;s\n", days_names[1]);
    ... prints "Sun".

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Elysia View Post
    Why on Earth would anyone want the week to start on Sunday!?!
    Well, dunno what Monday, Tuesday actually mean, but in greek it is: Sunday(Lordsday actually), Seconday, Thirday, ..., Fifthday, something, something. So it kind of makes sense for me
    Besides, Mondays suck. Badly. They don't deserve to be first. At least with the sunday leading you can say that the week starts good and ends good and convince yourself that the rest of the week also has to be good...

    Also, having an extra 4byte pointer wastes more memory than a 1byte empty string
    Last edited by C_ntua; 10-09-2008 at 06:13 PM.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Code:
    const char *dayn[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}, **days_names = dayn - 1;
    Wow I LOVE that bit of code at the end there, thats really helpful, do you mind explaining why its a double pointer

    Here is my understanding tell me if I am correct

    Since....dayn is already pointing to the const

    days_names is then pointing to dayn? so its like...

    days_names >>>>>>>> dayn >>>>>>>>>> --const--


    Yes no?


    EDIT:
    Quote Originally Posted by C_ntua View Post
    Also, having an extra 4byte pointer wastes more memory than a 1byte empty string
    Do you mean just having a place holder in the beginning ie. { "EmptyString" "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
    Last edited by Iconate; 10-09-2008 at 10:33 PM. Reason: Adding

  13. #13
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Not quite, days_names is pointing to the memory location one before dayn, which may or may not be valid memory or memory owned by you. In other words, it's a bad idea and pretty useless if just to avoid using proper indexing. On the contrary, get comfortable with "Math-on-the-fly" when it comes to array indexing, because just about every well-known programming language uses a zero-based array indexing scheme.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Why on Earth would anyone want the week to start on Sunday!?!
    Because it's the standard in many, many countries . Is America different?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by zacs7 View Post
    > Why on Earth would anyone want the week to start on Sunday!?!
    Because it's the standard in many, many countries . Is America different?
    I don't live in America. It's Monday first here, and Sunday last...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumeration Issues
    By cdn_bacon in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2007, 02:26 PM
  2. Enumeration problem
    By baniakjr in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2006, 02:32 PM
  3. Having trouble implementing enumeration.
    By RP319 in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2005, 07:03 PM
  4. Enumeration help
    By CXHatchback in forum C Programming
    Replies: 3
    Last Post: 04-17-2003, 08:59 AM
  5. enumeration
    By C-Struggler in forum C Programming
    Replies: 5
    Last Post: 03-13-2003, 09:36 AM