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

enum question

These code snippets come from a Dhrystone test we are using to evaluate ARM MDK and some ARM devices. The program works well but I have run across a construct that I do not understand. I hope some of you experts can point out what I'm missing.

In a header file an enum-
typedef enum {Ident_1,Ident_2,Ident_3,Ident_4} Enumeration;

In main() there is a declaration-
Enumeration Func_1();
/* forward declaration since Enumeration may not be int */

I have looked in my 'C' books and googled enum and Enumeration but failed to find any construct examples such as this. Of course I found the normal 'day of the week' and 'color' examples in many locations.

I have no problems with the code. I just don't understand the constructs.

Any guidance would be appreciated.
Bradford

  • Well, I'm not sure that the construct is standards-compliant (C90 or C99.) But the meaning is clear. Rather than declaring an enumeration type like this:

    enum Enumeration {Ident_1,Ident_2,Ident_3,Ident_4};
    


    and refer to it later as enum Enumeration, the author of the code chose to declare an anonymous enumeration and assign to it the type name Enumeration with the typedef. The return value of the function is of the type Enumeration.

  • Thank you Mikei.
    Your explanation makes perfect sense to me. Thank you for clearing the confusion from my pea size brain.
    Bradford

  • I keep forgetting to correct the name to Mike. The helpful feature of the browser keeps multiplying the typo...

  • Sorry Mike about the name mis-spell. Please blame it on my dyslexia of the fingers.
    Thanks again for the enum explanation.
    Bradford

  • "In a header file an enum-
    typedef enum {Ident_1,Ident_2,Ident_3,Ident_4} Enumeration;

    An easy way to manage typedef syntax is to first define an object of the type (ideally with the object name being what you want to name the type); for example:

    enum {Ident_1,Ident_2,Ident_3,Ident_4} Enumeration;
    

    With that you'd have an int object named "Enumeration" whose values can have those enumerated in the list (in this case 0-3).

    Now simply prefix all that with "typedef". Now "Enumeration" is not a space-allocating object, but rather a type named "Enumeration" with the same type constraints that the former object had.

  • Sorry, and in the context of the OP, take the "typedef" prefix off -- now what type is the object?

  • Thanks Dan for the additional information.
    The following comment in the code indicates the return may not be a standard enum int and I believe if we left the typedef we would expect a return of int.

    /* forward declaration since Enumeration may not be int */

    I'm not sure I fully understand the forward declaration but following the code there are many lines with a definite taste of ADA. So, it would appear the test code is written to support a number of different compilers.
    I don't plan to go deeper into the code at this time. We can run the code against different compiler/ARM implementations and get reasonable results.
    It's a tool and it works so I'll leave it be.
    Thanks again for the additional insight.
    Bradford

  • I think I more clearly understand your question and the intent of the code. From The Standard:

    "An implementation may delay the choice of which integer type until all enumeration constants have
    been seen."

    So, my take on it is that "your" coders were trying to "forward declare" a range-limited enumeration that would allow an implementation to choose (and possibly narrow) the size of the enumerated type.