how do you print a typedef enum?

This is a discussion on how do you print a typedef enum? within the C Programming forums, part of the General Programming Boards category; Say that I have the following: Code: typedef enum {x,y,z} Type; typedef struct treeNode { ................. Type type; ................ } ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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, 11:36 AM
  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, 07:26 PM
  4. Conflicting types of typedef error
    By advocation in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2005, 05:26 PM
  5. typedef problem
    By dr$brown in forum C Programming
    Replies: 6
    Last Post: 12-25-2004, 11:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21