简体   繁体   中英

How can I access the members of a struct in a loop?

Sort version: I have a for-loop, and in each iteration I want to access a different member of a struct (in sequence). Is this possible in C++? I remember something like it being possible back when I studied this sort of thing in school, but I may have been thinking of a lower level language/system.

Details: this is my struct:

struct meg_data_packet { /* decodes a meg data packet */
    struct data_value channel0;
    struct data_value channel1;
    struct data_value channel2;
    struct data_value channel3;
    struct data_value channel4;
    struct data_value channel5;
    struct data_value channel6;
    struct data_value channel7;
    struct data_value channel8;
    struct data_value channel9;
    struct data_value channel10;
    struct data_value channel11;
    struct data_value channel12;
    struct data_value channel13;
    struct data_value channel14;
    struct data_value channel15;
    char reset_flag0;
    char reset_flag1;
    char reset_flag2;
    char reset_flag3;
    char reset_flag4;
    char reset_flag5;
    char reset_flag6;
    char reset_flag7;
    char reset_flag8;
    char reset_flag9;
    char reset_flag10;
    char reset_flag11;
    char reset_flag12;
    char reset_flag13;
    char reset_flag14;
    char reset_flag15;
    uint32_t sequence_number;
};

data_value is a simple struct that just holds a uint32_t. I want to be able to access the members in a similar way to meg_data_packet[0] getting channel0 , [1] getting channel1 and so on.

Edit: Wow those are some fast replies. I should have been more clear. I can't use an array due to how the struct is being initialized. It is basically just a reskin of an existing data structure (actually an array of u_char's, I'm taking information from a system that is sent via tcp sockets). I'm casting the old data structure to this one to take advantage of it's structure, since I need access to the real values the u_char's represent.

You can use arrays like this:

struct meg_data_packet { /* decodes a meg data packet */
    data_value channels[16];
    char reset_flags[16];
    uint32_t sequence_number;
};

Or better still std::array

struct meg_data_packet { /* decodes a meg data packet */
    std::array<data_value, 16> channels;
    std::array<char, 16> reset_flags;
    uint32_t sequence_number;
};

Access the elements like this:

meg_data_packet mdp =get_packet();
mdp.channels[0] = ... // access by index 0-15

Yes, it's possible. Initialize an array of pointer offsets using offsetof . Then add that to reinterpret_cast<char*>(&struct_var) , cast that to the target type, and there you go...you have a pointer to the member and you can iterate them in sequence.

Of course you're going to need two loops, one for data_values and one for reset_flags.

I should also note that this is not how you should really be doing things in C++. This is the C way. C++ has more and better to offer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM