Enum problem...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rohit

    Enum problem...

    Is there any way by which I can find number of elements in an enum
    list?
    Generally I use one last element at the end to find out total number
    of elements. e.g.
    typedef enum{
    SUN,
    MON,
    TUE,
    EndOfList
    }weekdays;

    But I am facing a situation in which I need to know number of elements
    in an enum, which do not have
    EndofList as its last element. For backward compatibility reasons I
    can not change enum declaration which and add extra element.

    In case of arrays I can use sizeof(array)/sizeof(array element
    type)....But that wont work here....

    Anybody has a solution??

    Rohit
  • Chris Dollin

    #2
    Re: Enum problem...

    Rohit wrote:
    Is there any way by which I can find number of elements in an enum
    list?
    Not from within the code, no. (Do you mean "number of elements" or
    "value of last element" or "maximum value of elements"? They can all
    be different [1].)
    Anybody has a solution??
    Write a program (or script) which reads the enum definition and counts
    the number of elements, then writes out a fragment of C code to
    include, or fills values from a template.

    As usual, if you tell us more about the problem, we may be able to
    provide a better solution.

    [1] enum dubious
    {
    iffy = 128,
    hinky = 1
    };

    --
    'It changed the future .. and it changed us.' /Babylon 5/

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN

    Comment

    • CBFalconer

      #3
      Re: Enum problem...

      Rohit wrote:
      >
      Is there any way by which I can find number of elements in an
      enum list? Generally I use one last element at the end to find
      out total number of elements. e.g.
      typedef enum {
      SUN,
      MON,
      TUE,
      EndOfList
      } weekdays;
      >
      But I am facing a situation in which I need to know number of
      elements in an enum, which do not have EndofList as its last
      element. For backward compatibility reasons I can not change
      enum declaration which and add extra element.
      If you don't use the '=' operator in the declaration, the value of
      the last enumerated item + 1 is the count of items. In the above,
      that would be:

      1 + EndOfList

      so it is advisable to write the declaration in such a manner that
      you can easily add to it without changing the last item. E.g:

      typedef enum { SUN
      ,MON
      ,TUE
      ,EndOfList} weekdays;

      --
      [mail]: Chuck F (cbfalconer at maineline dot net)
      [page]: <http://cbfalconer.home .att.net>
      Try the download section.

      Comment

      • Rohit

        #4
        Re: Enum problem...

        On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@h p.comwrote:
        Rohit wrote:
        Is there any way by which I can find number of elements in an enum
        list?
        >
        Not from within the code, no. (Do you mean "number of elements" or
        "value of last element" or "maximum value of elements"? They can all
        be different [1].)
        OK, here I mean number of elements. I intend to use "number of
        elements" in an array declaration, so that array always has same
        number of element as there are elements in the enum in question.
        From your answer I suppose its not possible to do this. But still I
        feel some way should be there, coz it seems to be such an obvious
        problem.
        >
        Anybody has a solution??
        >
        Write a program (or script) which reads the enum definition and counts
        the number of elements, then writes out a fragment of C code to
        include, or fills values from a template.
        Frankly speaking I did not catch it. Reading the enum definition and
        counting number of elements ????
        How can you read a enum definition. Its not an array that you can
        parse through. Please elaborate on it.
        As usual, if you tell us more about the problem, we may be able to
        provide a better solution.
        >
        [1] enum dubious
            {
              iffy = 128,
              hinky = 1
            };
        >
        --
        'It changed the future .. and it changed us.'              /Babylon 5/
        >
        Hewlett-Packard Limited registered office:                Cain Road, Bracknell,
        registered no: 690597 England                                   Berks RG12 1HN

        Rohit

        Comment

        • Chris Dollin

          #5
          Re: Enum problem...

          Rohit wrote:
          On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@h p.comwrote:
          >Rohit wrote:
          Is there any way by which I can find number of elements in an enum
          list?
          >>
          >Not from within the code, no. (Do you mean "number of elements" or
          >"value of last element" or "maximum value of elements"? They can all
          >be different [1].)
          OK, here I mean number of elements.
          OK.
          I intend to use "number of
          elements" in an array declaration, so that array always has same
          number of element as there are elements in the enum in question.
          The number of named values, yes. (Note that without additional
          information, that none of the enums are defined using the `= constant`
          syntax, you don't know if you can use the enum values as indexes
          into that array. How you cope with that depends on what you're
          actually trying to do, which I still don't know.)
          From your answer I suppose its not possible to do this.
          Not from inside the code using Standard features.
          But still I feel some way should be there,
          Life is not perfect.
          coz it seems to be such an obvious problem.
          It doesn't happen often enough to matter, I suppose, since
          there are work-arounds of various kinds.
          Anybody has a solution??
          >>
          >Write a program (or script) which reads the enum definition and counts
          >the number of elements, then writes out a fragment of C code to
          >include, or fills values from a template.
          Frankly speaking I did not catch it. Reading the enum definition and
          counting number of elements ????
          Yes.
          How can you read a enum definition. Its not an array that you can
          parse through.
          No, but it will be the content of a file you can read. You don't
          have to write a script-or-program that reads C in its full glory,
          only something that reads the enum declarations that you are
          interested in.

          --
          'It changed the future .. and it changed us.' /Babylon 5/

          Hewlett-Packard Limited registered office: Cain Road, Bracknell,
          registered no: 690597 England Berks RG12 1HN

          Comment

          • aburry@ieee.org

            #6
            Re: Enum problem...

            On Sep 18, 12:10 pm, Chris Dollin <chris.dol...@h p.comwrote:
            Rohit wrote:
            >
            How can you read a enum definition. Its not an array that you can
            parse through.
            >
            No, but it will be the content of a file you can read. You don't
            have to write a script-or-program that reads C in its full glory,
            only something that reads the enum declarations that you are
            interested in.
            Like this, only much much better:

            /* enum.c */
            #include <stdio.h>

            typedef enum {
            MON,
            TUE,
            WED } weekdays;

            int main( int argc, char* argv[] ) {
            printf("%d\n", NUMDAYS);
            return 0;
            }
            /* enum.c */

            cat enum.c | tr '\n' ' ' | sed "s/;/;\n/g" | sed "s/enum/\nenum/g" |
            grep enum | grep weekdays | sed 's/[^{]*{\([^}]*\).*/\1/g' | sed "s/
            [^,]//g" | wc -c

            Then your build would define it at compile time like:
            gcc -DNUMDAYS=the_va l_you_calculate d -c enum.c

            Adam

            Comment

            • August Karlstrom

              #7
              Re: Enum problem...

              Rohit wrote:
              Is there any way by which I can find number of elements in an enum
              list?
              Generally I use one last element at the end to find out total number
              of elements. e.g.
              typedef enum{
              SUN,
              MON,
              TUE,
              EndOfList
              }weekdays;
              >
              But I am facing a situation in which I need to know number of elements
              in an enum, which do not have
              EndofList as its last element. For backward compatibility reasons I
              can not change enum declaration which and add extra element.
              What is the underlying problem that you are trying to solve? Maybe there
              is a simpler solution.


              August

              Comment

              • James Kuyper

                #8
                Re: Enum problem...

                Rohit wrote:
                On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@h p.comwrote:
                >Rohit wrote:
                >>Is there any way by which I can find number of elements in an enum
                >>list?
                >Not from within the code, no. (Do you mean "number of elements" or
                >"value of last element" or "maximum value of elements"? They can all
                >be different [1].)
                OK, here I mean number of elements. I intend to use "number of
                elements" in an array declaration, so that array always has same
                number of element as there are elements in the enum in question.
                From your answer I suppose its not possible to do this. But still I
                feel some way should be there, coz it seems to be such an obvious
                problem.
                Actually, it isn't. When you use enumerations properly, there's seldom a
                need to know how many elements there are in an enumeration. If you could
                explain why you think you need that information, I suspect that we can
                suggest a way to deal with the problem; it might involve re-designing
                your code.

                Comment

                • Lowell Gilbert

                  #9
                  Re: Enum problem...

                  James Kuyper <jameskuyper@ve rizon.netwrites :
                  Rohit wrote:
                  >On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@h p.comwrote:
                  >>Rohit wrote:
                  >>>Is there any way by which I can find number of elements in an enum
                  >>>list?
                  >>Not from within the code, no. (Do you mean "number of elements" or
                  >>"value of last element" or "maximum value of elements"? They can all
                  >>be different [1].)
                  >OK, here I mean number of elements. I intend to use "number of
                  >elements" in an array declaration, so that array always has same
                  >number of element as there are elements in the enum in question.
                  >From your answer I suppose its not possible to do this. But still I
                  >feel some way should be there, coz it seems to be such an obvious
                  >problem.
                  >
                  Actually, it isn't. When you use enumerations properly, there's seldom
                  a need to know how many elements there are in an enumeration. If you
                  could explain why you think you need that information, I suspect that
                  we can suggest a way to deal with the problem; it might involve
                  re-designing your code.
                  In the case described by the original poster, the usual purpose is to
                  use the enumeration values to index into the array. In this case,
                  what is really wanted is, indeed, the maximum value of the elements
                  (plus one...). I think that the original poster needs to explain how
                  the problem at hand is different than this common case, because
                  someone (quite possibly me) is clearly having trouble understanding
                  the underlying situation.

                  --
                  Lowell Gilbert, embedded/networking software engineer

                  Comment

                  • gw7rib@aol.com

                    #10
                    Re: Enum problem...

                    On 18 Sep, 12:57, Rohit <papakap...@gma il.comwrote:
                    Is there any way by which I can find number of elements in an enum
                    list?
                    Generally I use one last element at the end to find out total number
                    of elements. e.g.
                    typedef enum{
                    SUN,
                    MON,
                    TUE,
                    EndOfList
                    >
                    }weekdays;
                    >
                    But I am facing a situation in which I need to know number of elements
                    in an enum, which do not have
                    EndofList as its last element. For backward compatibility reasons I
                    can not change enum declaration which and add extra element.
                    If "backward compatibility reasons" means "it's in a file which *must
                    not*, repeat *must not*, be changed, ever - then surely all you need
                    to do is write code that is compatible with that file?

                    If the enum might get changed, why can't you change it?

                    Comment

                    • Jack Klein

                      #11
                      Re: Enum problem...

                      On Thu, 18 Sep 2008 04:57:52 -0700 (PDT), Rohit <papakapapa@gma il.com>
                      wrote in comp.lang.c:
                      Is there any way by which I can find number of elements in an enum
                      list?
                      Generally I use one last element at the end to find out total number
                      of elements. e.g.
                      typedef enum{
                      SUN,
                      MON,
                      TUE,
                      EndOfList
                      }weekdays;
                      >
                      But I am facing a situation in which I need to know number of elements
                      in an enum, which do not have
                      EndofList as its last element. For backward compatibility reasons I
                      can not change enum declaration which and add extra element.
                      There are _usually_, but not always, ways around compatibility
                      problems.

                      If "backwards compatibility reasons" means that you literally cannot
                      physically modify the source code file at all, then the following will
                      not help you. But if "backwards compatibility reasons" only means
                      that you cannot change the definition of the enumeration type as SEEN
                      by already existing source file, try this:

                      typedef enum{ /* existing line */
                      SUN, /* existing line */
                      MON, /* existing line */
                      TUE /* existing line */
                      #ifdef ALLOW_TOTAL_VAL UE /* new line 1 */
                      , EndOfList /* new line 2 */
                      #endif /* new line 3 */
                      } weekdays; /* existing line */

                      ....then in your new source files that need the total, you define
                      ALLOW_TOTAL_VAL UE before including the header. The legacy source does
                      not define the macro, and therefore does not see the new member.
                      In case of arrays I can use sizeof(array)/sizeof(array element
                      type)....But that wont work here....
                      >
                      Anybody has a solution??
                      Another, somewhat messier solution involves not modifying the original
                      header at all, if that is absolutely forbidden.

                      Copy the contents of the file to another file, with a different but
                      similar name. Make your additions to the enum declaration in that
                      file, leaving all the other contents the same. In your code, include
                      the new variation.

                      This gets messy if the original header is ever updated, but perhaps
                      for "backwards compatibility reasons" that won't happen often, or at
                      all.

                      You could actually write a little program in C, Perl, or whatever,
                      that automatically makes the modified copy of the original header, and
                      your build system could automatically run this program if the original
                      header is changed.

                      There's almost always a solution if you're willing to think "outside
                      the box", an expression I truly dislike.

                      --
                      Jack Klein
                      Home: http://JK-Technology.Com
                      FAQs for
                      comp.lang.c http://c-faq.com/
                      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                      alt.comp.lang.l earn.c-c++

                      Comment

                      • Bartc

                        #12
                        Re: Enum problem...


                        "Lowell Gilbert" <lgusenet@be-well.ilk.orgwro te in message
                        news:44d4j1ckgz .fsf@be-well.ilk.org...
                        James Kuyper <jameskuyper@ve rizon.netwrites :
                        >
                        >Rohit wrote:
                        >>On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@h p.comwrote:
                        >>>Rohit wrote:
                        >>>>Is there any way by which I can find number of elements in an enum
                        >>>>list?
                        In the case described by the original poster, the usual purpose is to
                        use the enumeration values to index into the array. In this case,
                        what is really wanted is, indeed, the maximum value of the elements
                        (plus one...). I think that the original poster needs to explain how
                        the problem at hand is different than this common case, because
                        someone (quite possibly me) is clearly having trouble understanding
                        the underlying situation.
                        OK, so how do you get the maximum value of the elements, plus one?

                        In a way that is independent of the source code for the enum (eg. weekdays)
                        being changed?

                        Example:

                        enum (red,green,blue ,yellow} colours;

                        Imported into another source file:

                        #include "colours.h"

                        #define NOCOLOURS (yellow+1)
                        int a[NOCOLOURS];
                        for (i=0; i<NCOLOURS; ++i) ...

                        This will break as soon as more colours are added to the enum.

                        --
                        Bartc

                        Comment

                        • Ian Collins

                          #13
                          Re: Enum problem...

                          Bartc wrote:
                          >
                          "Lowell Gilbert" <lgusenet@be-well.ilk.orgwro te in message
                          news:44d4j1ckgz .fsf@be-well.ilk.org...
                          >James Kuyper <jameskuyper@ve rizon.netwrites :
                          >>
                          >>Rohit wrote:
                          >>>On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@h p.comwrote:
                          >>>>Rohit wrote:
                          >>>>>Is there any way by which I can find number of elements in an enum
                          >>>>>list?
                          >
                          >In the case described by the original poster, the usual purpose is to
                          >use the enumeration values to index into the array. In this case,
                          >what is really wanted is, indeed, the maximum value of the elements
                          >(plus one...). I think that the original poster needs to explain how
                          >the problem at hand is different than this common case, because
                          >someone (quite possibly me) is clearly having trouble understanding
                          >the underlying situation.
                          >
                          OK, so how do you get the maximum value of the elements, plus one?
                          >
                          In a way that is independent of the source code for the enum (eg.
                          weekdays) being changed?
                          >
                          You can't.

                          --
                          Ian Collins.

                          Comment

                          • Lowell Gilbert

                            #14
                            Re: Enum problem...

                            "Bartc" <bc@freeuk.comw rites:
                            "Lowell Gilbert" <lgusenet@be-well.ilk.orgwro te in message
                            news:44d4j1ckgz .fsf@be-well.ilk.org...
                            >James Kuyper <jameskuyper@ve rizon.netwrites :
                            >>
                            >>Rohit wrote:
                            >>>On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@h p.comwrote:
                            >>>>Rohit wrote:
                            >>>>>Is there any way by which I can find number of elements in an enum
                            >>>>>list?
                            >
                            >In the case described by the original poster, the usual purpose is to
                            >use the enumeration values to index into the array. In this case,
                            >what is really wanted is, indeed, the maximum value of the elements
                            >(plus one...). I think that the original poster needs to explain how
                            >the problem at hand is different than this common case, because
                            >someone (quite possibly me) is clearly having trouble understanding
                            >the underlying situation.
                            >
                            OK, so how do you get the maximum value of the elements, plus one?
                            >
                            In a way that is independent of the source code for the enum
                            (eg. weekdays) being changed?
                            >
                            Example:
                            >
                            enum (red,green,blue ,yellow} colours;
                            >
                            Imported into another source file:
                            >
                            #include "colours.h"
                            >
                            #define NOCOLOURS (yellow+1)
                            int a[NOCOLOURS];
                            for (i=0; i<NCOLOURS; ++i) ...
                            >
                            This will break as soon as more colours are added to the enum.
                            The traditional approach is to put a sentinel value into the enumeration, at
                            the end, and leave the responsibility for making sure it stays the
                            largest value on whoever changes it. This doesn't literally avoid the
                            issue, but it works fairly consistently in practice. When concerned
                            that it's unclear, one can always add a comment to bring it to the
                            next programmer's attention.

                            --
                            Lowell Gilbert, embedded/networking software engineer

                            Comment

                            Working...