Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Enumerated Type Not Comparing Correctly

Enumerated Type Not Comparing Correctly

Scheduled Pinned Locked Moved C / C++ / MFC
helphardwaretutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jim Fell
    wrote on last edited by
    #1

    Hello. I'm writing some C code for an embedded application, and I've run into a problem wherein a compare against an enumerated value is not being executed correctly. Take the following code snippet, for example:

    typedef unsigned int UINT16;
    typedef enum enum_items_tag
    {
    ITEM_1,
    ITEM_2,
    ITEM_3,

    /* ... */

    ITEM_918,
    MAX_ENUM_ITEMS
    } enum_items_t;

    UINT16 n;

    for ( n = 0; n < MAX_ENUM_ITEMS; n++ )
    {
    // Do something
    }

    The code executes as expected, until n is incremented to equal MAX_ENUM_ITEMS, at which time the compare fails, and execution continues within the loop (when it should have exited). I've done things like this in the past without any problems. I've tried re-typing n as enum_items_t (i.e. declaring n as "enum_items_t n"), as well as type casting MAX_ENUM_ITEMS as UINT16. The only other thing I can think of at this point is that maybe there is an issue with the number of items there are in my enumerated type (919). Does anyone know if there are such constraints on enumerated types? I'm using a GCC based compiler. Or, if you have any other ideas, it would be much appreciated. Thanks.

    I C J 3 Replies Last reply
    0
    • J Jim Fell

      Hello. I'm writing some C code for an embedded application, and I've run into a problem wherein a compare against an enumerated value is not being executed correctly. Take the following code snippet, for example:

      typedef unsigned int UINT16;
      typedef enum enum_items_tag
      {
      ITEM_1,
      ITEM_2,
      ITEM_3,

      /* ... */

      ITEM_918,
      MAX_ENUM_ITEMS
      } enum_items_t;

      UINT16 n;

      for ( n = 0; n < MAX_ENUM_ITEMS; n++ )
      {
      // Do something
      }

      The code executes as expected, until n is incremented to equal MAX_ENUM_ITEMS, at which time the compare fails, and execution continues within the loop (when it should have exited). I've done things like this in the past without any problems. I've tried re-typing n as enum_items_t (i.e. declaring n as "enum_items_t n"), as well as type casting MAX_ENUM_ITEMS as UINT16. The only other thing I can think of at this point is that maybe there is an issue with the number of items there are in my enumerated type (919). Does anyone know if there are such constraints on enumerated types? I'm using a GCC based compiler. Or, if you have any other ideas, it would be much appreciated. Thanks.

      I Offline
      I Offline
      includeh10
      wrote on last edited by
      #2

      why do not output value of MAX_ENUM_ITEMS to see what it is, you may miss some thing in the middle of the enum. totally 919 items? MAX_ENUM_ITEMS should be 918.

      J 1 Reply Last reply
      0
      • I includeh10

        why do not output value of MAX_ENUM_ITEMS to see what it is, you may miss some thing in the middle of the enum. totally 919 items? MAX_ENUM_ITEMS should be 918.

        J Offline
        J Offline
        Jim Fell
        wrote on last edited by
        #3

        You're right. Good catch! The enumerated type enum_items_t has a total of 920 items, including MAX_ENUM_ITEMS. I was thinking in terms of offset, or what n would be as the loop repeats. Unfortunately, outputting constants information with embedded programming can be a little tricky, especially when you're stuck using a ***** compiler/IDE. I'll see what I can do, though. Thanks.

        1 Reply Last reply
        0
        • J Jim Fell

          Hello. I'm writing some C code for an embedded application, and I've run into a problem wherein a compare against an enumerated value is not being executed correctly. Take the following code snippet, for example:

          typedef unsigned int UINT16;
          typedef enum enum_items_tag
          {
          ITEM_1,
          ITEM_2,
          ITEM_3,

          /* ... */

          ITEM_918,
          MAX_ENUM_ITEMS
          } enum_items_t;

          UINT16 n;

          for ( n = 0; n < MAX_ENUM_ITEMS; n++ )
          {
          // Do something
          }

          The code executes as expected, until n is incremented to equal MAX_ENUM_ITEMS, at which time the compare fails, and execution continues within the loop (when it should have exited). I've done things like this in the past without any problems. I've tried re-typing n as enum_items_t (i.e. declaring n as "enum_items_t n"), as well as type casting MAX_ENUM_ITEMS as UINT16. The only other thing I can think of at this point is that maybe there is an issue with the number of items there are in my enumerated type (919). Does anyone know if there are such constraints on enumerated types? I'm using a GCC based compiler. Or, if you have any other ideas, it would be much appreciated. Thanks.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Just curiosity: what is the usefulness of such a enum? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          J 1 Reply Last reply
          0
          • C CPallini

            Just curiosity: what is the usefulness of such a enum? :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            J Offline
            J Offline
            Jim Fell
            wrote on last edited by
            #5

            It's used for quickly indexing into an array, rather than having to use processor time to search for an element.

            C 1 Reply Last reply
            0
            • J Jim Fell

              Hello. I'm writing some C code for an embedded application, and I've run into a problem wherein a compare against an enumerated value is not being executed correctly. Take the following code snippet, for example:

              typedef unsigned int UINT16;
              typedef enum enum_items_tag
              {
              ITEM_1,
              ITEM_2,
              ITEM_3,

              /* ... */

              ITEM_918,
              MAX_ENUM_ITEMS
              } enum_items_t;

              UINT16 n;

              for ( n = 0; n < MAX_ENUM_ITEMS; n++ )
              {
              // Do something
              }

              The code executes as expected, until n is incremented to equal MAX_ENUM_ITEMS, at which time the compare fails, and execution continues within the loop (when it should have exited). I've done things like this in the past without any problems. I've tried re-typing n as enum_items_t (i.e. declaring n as "enum_items_t n"), as well as type casting MAX_ENUM_ITEMS as UINT16. The only other thing I can think of at this point is that maybe there is an issue with the number of items there are in my enumerated type (919). Does anyone know if there are such constraints on enumerated types? I'm using a GCC based compiler. Or, if you have any other ideas, it would be much appreciated. Thanks.

              J Offline
              J Offline
              Jim Fell
              wrote on last edited by
              #6

              I found the problem. As usual, it turned out to be really simple. My INVALID_ITEM item was positioned at the beginning of the enumeration. When I moved it to the end, after MAX_ENUM_ITEMS, things lined up and started working.

              1 Reply Last reply
              0
              • J Jim Fell

                It's used for quickly indexing into an array, rather than having to use processor time to search for an element.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Well, I cannot see any speed advantage. Would you please elaborate a bit? :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups