How to print address of enum?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ankit Khare
    New Member
    • Mar 2011
    • 19

    How to print address of enum?

    As i read size of enum is sufficient enough to hold any integer value,thus its 4 bytes in my machine.
    Code:
    typedef enum{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
    }weekday;
    
    
    void main()
    {
            weekday day=Tuesday;
            printf("%d\n",day);// prints 1,correct
            printf("%d\n",Wednesday);//Prints 2!How do i have access to this?It prints the correct value
    
            //But when i am traying to print the address of the following
            printf("%p\n",&day);//prints some address,correct
            //printf("%p\n",&Wednesday);if i try to print this it says 'lvalue required as unary ‘&’ operand'
    
    }
    ~                                                                                                                            
    ~
    So,enum variable gets sizeof(int),i can even access any of its member directly by name,but i cant print there addresses.?
  • jjdot
    New Member
    • Feb 2011
    • 11

    #2
    enum are not variables. They are constant value not constant variables ! A variable has a memory address;
    a constant, no. What you try to do is the same that trying to get the address of 2.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      More specifically, and enum is a name for an integer value:

      Code:
      enum VALUE  {BLUE=1};
      You can now use BLUE in your code instead of hard-coding a 1. The idea is that if 1 changes to 2 all you to do is change the enum and recompile instead of editing all of the code changing al 1's to 2 except for the 1's that are supposed to be 1's.

      Because BLUE is a name there is no address. The address comes in when you have a variable with the value BLUE.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Also enums are not necessarily the size of an int (although they might be on your system).

        It is permissible for the compiler to choose any integer type to hold an enum as long as that integer type can hold all the values defined for an enum. For example with the enum in your code it only defines values 0 - 6 so a compiler that displayed this behaviour might use a char for that enum.

        For a given compiler a given enum will always use the same basic type.

        Comment

        • Ankit Khare
          New Member
          • Mar 2011
          • 19

          #5
          So where are these names to these enum constants are stored?Suppose if i have defined enum but i have'nt created a variable of it,then when i am trying to access any member of it,i still get the right value.Where are these names fetched from?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            The compiler stores them in memory while it is compiling the program.

            Comment

            Working...