Indicate the member of the struct

This is a discussion on Indicate the member of the struct within the C Programming forums, part of the General Programming Boards category; Dear all, I have a struct with many members (called struct X). Now I want to use an integer (for ...

  1. #1
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46

    Indicate the member of the struct

    Dear all,
    I have a struct with many members (called struct X).
    Now I want to use an integer (for e.g, int index) to index the struct.

    For details,
    Originally, we access the member of struct by using:
    Code:
    X.member1000
    Now, I want to write a function like that:
    Code:
    void my_function (int index, void * output)
    For example, I call:
    Code:
    my_function (1000, output)
    This function will return the pointer of X.member1000 into the pointer output.

    Can I do like this?
    Is there the way to point to a member of struct? For e.g, can I use X.1000 (or something like this) to point to the 1000th member of struct X?

    Any help is welcome

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    structName[index].member.

  3. #3
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Hi Adak,
    Thanks for your idea, but I don't ask about an array of struct.
    I am asking about a struct which has many members, such as:
    Code:
    struct A
    {
     int member1,
     int member2,
    ...
     int member10000
    }
    So, how to access A.member2000 fast

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    It would be possible if every member was the same type, then you could do
    Code:
    int *p = A.member1 + n ;
    However, there is no guarantee that the members exist as contiguous memory locations without gaps.

    Besides, if you're limited to the same data type, then you should be using an array as member.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Unless you allocated their memory, (in which case you'd work through a pointer, so there would be a small slowdown), all struct members are contiguous. (Some members may include padding, but that is still part of the member, so they're still contiguous.)

    What makes you think that access to a struct member is slow? It's a base address and an offset, similar to an array (except as noted above, which can also be the case with an array).

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    "..on many systems there are gaps between the members, since certain machine instructions require data types to be stored at particular addresses." - Johnsonbaugh & Kalin, 2.ed

    So, I guess if they are all the same data type then one can expect it to be contiguous.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    If the 1000+ members of interest are all the same type, create an array in the struct.
    Code:
    struct X
    {
        int member[1000];
    };
    Then, is x is a variable of type X, the n'th member may be accessed using "x.member[n]". Just bear in mind n starts at 0 not 1.

    If you have a struct containing 1000 members, of varying types, then you need to revisit your design. Particularly if the types are of varying sizes, or if there is potential for padding between them.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Tclausex View Post
    "..on many systems there are gaps between the members, since certain machine instructions require data types to be stored at particular addresses." - Johnsonbaugh & Kalin, 2.ed

    So, I guess if they are all the same data type then one can expect it to be contiguous.
    Right, that's the "padding", but who cares? It's an address to any member of the struct, which is the base address + an offset. One look up, one addition, and one jump.

    Have to agree with Grumpy though - a struct with hundreds or thousands of members, should be in an array of it's own -- most probably.

  9. #9
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear all,
    The idea of member index is very good, but unfortunately, the data type is various, not the same.
    If the data type is the same, the simple way is create an array such as member [10000], it is better than use member1 member2 ... member10000

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by Hannibal2010 View Post
    Dear all,
    The idea of member index is very good, but unfortunately, the data type is various, not the same.
    So, let's say you have 1000 members of varying types and, more importantly, sizes. The sizes of every type (except char types, which have a size defined to be 1) will vary between compilers (and, even with compiler settings) - because the sizes (of int, short, float, enum types, nested structs) are ALL implementation defined. Furthermore, in a struct, the compiler may introduce varying levels of padding between the members - and again, the amount of padding between any two members that are adjacent in memory is implementation defined.

    Without knowing what types you are going to place in the struct, and in what order, how would you expect to be able to compute the address of member number 600? Even if you could compute that address and return it, what would you expect the caller to do with the returned address if it doesn't know the size of member 600?
    Right 98% of the time, and don't care about the other 3%.

  11. #11
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Hi grumpy,
    So that is what I mean - I don't expect about memory size approach. I hope that C programming language has a special method to access to member of struct without care about member size.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    No, it does not. That is why I asked those questions. They will need to be answered to provide what you are asking for.
    Right 98% of the time, and don't care about the other 3%.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Quote Originally Posted by Adak View Post
    What makes you think that access to a struct member is slow?
    I'd like an answer to that as well.

    Also if the type is called A, you can't just do A.xxx, you have to create an instance of the struct and access that.

    Aside: Is this an experiment? Surely there are better variable names than member883458734 ... you should treat structures like a record of related data. So if possible, refrain from making a single struct with thousands of members.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    I suppose you could do something like -> offsetof(3): offset of structure member - Linux man page

    Code:
    struct structInfo {
      size_t  pos;
      size_t  size;
    } info[NUM_MEMBERS];
    
    info[0].pos = offsetof(A,member1);
    info[0].size = sizeof(A.member1);
    // and so on
    From that, you can create a pointer to any arbitrary member of your struct A, simply by indexing the info array.

    The size might give you a hint as to what type it is, but really you're on your own at that point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    There is also the problem of initialising that array, Salem. It cannot be done with a simple loop.

    Given the nature of the OP, there would probably be a desire to have that array automatically initialised. Which brings us back to the original problem.
    Right 98% of the time, and don't care about the other 3%.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assign struct member value to struct member value
    By thahemp in forum C Programming
    Replies: 5
    Last Post: 10-13-2010, 09:48 AM
  2. printf of struct member
    By dassybr in forum C Programming
    Replies: 3
    Last Post: 11-21-2009, 02:22 PM
  3. struct member assignment (c-only)
    By emorrp1 in forum C Programming
    Replies: 8
    Last Post: 06-25-2008, 05:30 AM
  4. struct member name prefixing
    By oogabooga in forum C Programming
    Replies: 3
    Last Post: 01-11-2008, 10:37 AM
  5. Replies: 1
    Last Post: 05-05-2004, 06:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21