Thread: Enum question

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    30

    Enum question

    Hi all!

    Is it possible somoehow to fetch the name of an enumerated var?

    eg: enum {foo=1,boo=2}

    Well, I would like to get 'foo' from this and not its value to find out if that name is enumerated.

    Is this possible, or stupid question? - (Most likely the latter...-

    Thanks
    Best Regards,

    Bill

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    30
    Hi,

    Too bad, it would be useful sometimes.. Anyway, thanks, then I leave it alone.

    Bye
    Best Regards,

    Bill

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Not from the enumeration, but you can make a string array to get it from:
    Code:
    enum {Alpha=0, Beta, Gamma};
    char* Names[] = {"Alpha", "Beta", "Gamma"};
    
    printf("%s", Names[Alpha]);
    Make sure your enumeration starts at 0.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is a variation of something I've used to do this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    enum EType
    {
        #define GET_ENUM
        #include "test.mh"
        MY_TOTAL
    };
    
    static const char *const EnumText [ MY_TOTAL ] =
    {
        #define GET_ENUM_TEXT
        #include "test.mh"
    };
    
    static const char *const Description [ MY_TOTAL ] =
    {
        #define GET_DESCRIPTION
        #include "test.mh"
    };
    
    int main(void)
    {
        enum EType i;
        for ( i = 0; i < MY_TOTAL; ++i )
        {
            printf("(%d) %-5s \"%s\"\n", i, EnumText [ i ], Description [ i ]);
        }
        return 0;
    }
    
    /* my output
    (0) ALPHA "This is a description of this element."
    (1) BETA  "The next item has no description."
    (2) GAMMA ""
    (3) DELTA ""
    */
    The multiply-included [test.mh] is like the following.
    Code:
    /* Macro Definition */
    #if   defined(GET_ENUM)
    #define MACRO(tag,desc)  tag,
    #elif defined(GET_ENUM_TEXT)
    #define MACRO(tag,desc)  #tag,
    #elif defined(GET_DESCRIPTION)
    #define MACRO(tag,desc)  desc,
    #else
    #define MACRO(tag,desc)
    #endif
    
    /* Macro Binding */
    MACRO(ALPHA, "This is a description of this element.")
    MACRO(BETA,  "The next item has no description.")
    MACRO(GAMMA, "")
    MACRO(DELTA, "")
    
    /* Macro Un-Definition */
    #undef MACRO
    
    #if   defined(GET_ENUM)
    #undef        GET_ENUM
    #elif defined(GET_ENUM_TEXT)
    #undef        GET_ENUM_TEXT
    #elif defined(GET_DESCRIPTION)
    #undef        GET_DESCRIPTION
    #endif
    There are other variations as well.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    30

    Wink

    Hi!

    Wow sounds good, I will try this. Thanks for the help for everyone!
    Best Regards,

    Bill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Question on Enum
    By markcls in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 08:19 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. enum question
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 12-30-2001, 12:04 AM