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

Length of a typedef enum

I am trying to pass the length of a typedefinition to an integer by using the sizeof function. At the moment I only get the value 4 passed back no matter the number of items are in the typedef.

This is an example of what I have done:

typedef enum {Spd1_PlusEPA_Motor_Type, Spd2_PlusEPA_Motor_Type, Spd2orEPAorSiemens_Motor_Type, Var_Speed_Man_Motor_Type, Var_Speed_Aut_Motor_Type, Var_IOBlast_Motor_Type, Var_Spd_Inl_Motor_Type, Var_Spd_Outl_Motor_Type} Motor_Types;

#define Motor_Type_Len sizeof(Motor_Types)

My guess sizeof is working 100% and it has just retuned the length of Motor_Types which is 4 bytes long.

Is there another command to finding the length or have I just implemented this incorrectly?

  • Were you expecting or desiring the 'length' to be 8 or are you asking about the implementation narrowing the enumerated type because its 8 enumeration constants?

  • There is no other command to find the length (as in number of distinct entries) in an enumerated type. The sizeof() operator returns the number of bytes required to store a value of that type, and does not reveal anything about what's contained by the type. One thing that's commonly done is to just always append an entry at the end of your list like Num_Motor_Types. As long as you don't manually assign any values to any of the enumerations, this will then always represent the number of entries in your list and can be used for range-checking, etc.

  • Thanks for the info.

    I had a feeling that was the case. I am pretty new to C programing and wanted to be sure there wasnt an actual command rather than using a work around.