Thread: how do you print a typedef enum?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    how do you print a typedef enum?

    Say that I have the following:

    Code:
    typedef enum {x,y,z} Type;   
    
    typedef struct treeNode   
       {
         .................
         Type type;    
         ................
       } TreeNode;
    and I have declared a treenode called test, why can't I do this?

    fprintf(stderr, "type is %s!\n", test->type)

    How can I do such thing? Is it possible?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by -EquinoX- View Post
    How can I do such thing? Is it possible?
    Not directly. You will have to make a table mapping the enum values to strings. Or, if your enum values are non-contiguous, a switch statement or explicit string of if-else.

    For instance:

    Code:
    enum myEnumType
    {
        enum1,
        enum2,
        enum3
    };
    
    const char *myEnumTypeNames[] =
    {
        "enum1",
        "enum2",
        "enum3"
    };
    
    void printEnumName(myEnumType val)
    {
        printf("%s\n", myEnumTypeNames[val]);
    }
    Yeah, it's a pain.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  2. String parser
    By sand_man in forum C Programming
    Replies: 13
    Last Post: 08-13-2005, 10:33 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. Conflicting types of typedef error
    By advocation in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2005, 06:26 PM
  5. typedef problem
    By dr$brown in forum C Programming
    Replies: 6
    Last Post: 12-25-2004, 12:27 PM