Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7587869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:46:58+00:00 2026-05-30T19:46:58+00:00

I know that when defining a an enum you can enumerate through the list

  • 0

I know that when defining a an enum you can enumerate through the list numerically:

typedef enum MONTH { Jan = 1, Feb, March, ... };

Can you enumerate through values in a struct the same way? I basically want to loop through the values in a struct using a for or while loop.

struct items {char *item_name, int item_value};

struct items Items_list[] =
{ 
    "item 1", 2000,
    "item 2", 3600,
    ....
};

Language used is C.

Edit: I may have just answered my own question since what I had in mind is an array of structs. Will leave the question up for now however.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:46:59+00:00Added an answer on May 30, 2026 at 7:46 pm

    This declaration and initializer combination are invalid. (The question changed while the original version of this answer was written.)

    If you are asking “is there a way to access the first member of the structure, then the second, without knowing the structure element names”, then the answer is ‘no, not without careful encoding beforehand’.


    The careful coding involves multiple steps. For each element, you need an encoding of the type, the offset of the member in the structure, and perhaps the size of the member (if the encoding of the type does not give that to you anyway):

    typedef enum { MT_INT, MT_CHAR_PTR, ... } MemberType;
    
    typedef struct MemberAccess
    {
        const char *name;
        size_t      offset;
        MemberType  type;
    } MemberAccess;
    
    static const MemberAccess members[] =
    {
        "item_name",  offsetof(struct items, item_name),  MT_CHAR_PTR },
        "item_value", offsetof(struct items, item_value), MT_INT      },
    };
    

    And now, with excruciating care, you can write code to either get or set the value in the Nth member of a struct item pointed at by a particular pointer. However, doing so is still far from trivial.

    int get_int(const void *data, const MemberAccess *member)
    {
        assert(member->type == MT_INT);
        return (*(const int *)((const char *)data + member->offset));
    }
    

    GCC notwithstanding, you need the cast to a character pointer; you cannot legitimately do pointer arithmetic on void *.

    You might then invoke:

    int value = get_int(&Items_list[1], &members[1]);
    

    to get at the integer value of the second field of the second element of the array.

    This is so excruciating to deal with that there have to be excellent reasons to go through the overhead. There can be such reasons. I know of a system with 400 configuration parameters (which is a problem in its own right, but lets pretend that’s OK; they’ve accumulated over 20 years of development) stored in a structure with heterogeneous types for the members. The code that manipulates it is written out 400 times – ouch! – because it doesn’t use a system driven off an analogue of the MemberAccess structure. The code would be a lot more compact than it currently is because there are about a dozen data types to deal with, so most of the code is repetitive. Another way of reducing the complexity of that code would be to make everything into a string, but there are issues with that transformation too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Know I can use Generics while defining the ArrayList to do that. But
I know that one of the defining principles of Test driven development is that
I know that I can do something like $int = (int)99; //(int) has a
I know that you can insert multiple rows at once, is there a way
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that IList is the interface and List is the concrete type but
Possible Duplicate: Interface defining a constructor signature? I know that you cannot specify a
With StructureMap, I know that you can wire constructor arguments to app settings when
I know that I can kick the the preprocessor to spit out output with
I know that theoretically one can put what he wants in the method, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.