This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Dont get it

Please can someone show me a code fragment using the following code. I think I understand the enum, and the union part. But dont get how the enum works in conjunction with the union part. Every other example I have found uses an integer in place of the enum to save the union type that's presently in use. I have tried to comprehend how this works but simply miss whats going on.

struct taggedunion {
enum {UNKNOWN, INT, LONG, DOUBLE, POINTER} code;
     union {
             int i;
             long l;
             double d;
             void *p;
     } u;
};

Parents
  • An enum is really just a way of assigning symbolic names to constant integer values; thus

    enum {UNKNOWN, INT, LONG, DOUBLE, POINTER}
    

    is almost equivalent to

    #define UNKNOWN 0
    #define INT 1
    #define LONG 2
    #define DOUBLE 3
    #define POINTER 4
    


    The difference is that enum is part of the 'C' language, but #defines are handled entirely by the preprocessor - so the compiler never gets to see the symbolic names. One advantage this gives is the opportunity for the compiler to include the symbolic names in its debug info - I don't know if Keil's ARM tools take advantage of this, but C51 doesn't :-(

    See c-faq.com/.../enumvsdefine.html

    Again, this is standard 'C' stuff - nothing specifically to do with ARM, Keil, or embedded systems.

Reply
  • An enum is really just a way of assigning symbolic names to constant integer values; thus

    enum {UNKNOWN, INT, LONG, DOUBLE, POINTER}
    

    is almost equivalent to

    #define UNKNOWN 0
    #define INT 1
    #define LONG 2
    #define DOUBLE 3
    #define POINTER 4
    


    The difference is that enum is part of the 'C' language, but #defines are handled entirely by the preprocessor - so the compiler never gets to see the symbolic names. One advantage this gives is the opportunity for the compiler to include the symbolic names in its debug info - I don't know if Keil's ARM tools take advantage of this, but C51 doesn't :-(

    See c-faq.com/.../enumvsdefine.html

    Again, this is standard 'C' stuff - nothing specifically to do with ARM, Keil, or embedded systems.

Children
No data