Dynamic access to components of a struct

This is a discussion on Dynamic access to components of a struct within the C Programming forums, part of the General Programming Boards category; Is there in C a way to access fields of a struct in a dynamic way? Look at this pseudo-code: ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    145

    Dynamic access to components of a struct

    Is there in C a way to access fields of a struct in a dynamic way? Look at this pseudo-code:

    Code:
    typedef struct {
    int c1;
    int c2;
    } t_struct1;
    
    typedef struct {
    int c1;
    int c2;
    int c3;
    } t_struct2;
    
    void DumpStruct(const void * s) {
    for (int i = 0; i < number_of_vars_in_struct; i++) {
        printf("Var %d\n",i);
        for (int x=0; x<sizeof(struct_field[i];x++) {
            printf("Byte %d = %d\n", x,s->field[i][x]);
        }
    }
    }
    
    
    main {
    t_struct1 p1;
    t_struct2 p2;
    DumpStruct(&p1);
    DumpStruct(&p2);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to be able to access variable size/type struct elements, then you would have to do something more advanced than that (a table of "type and offset" would work - I posted something like that not so long ago).

    If you are just after dumping the information to say "This is the content", I guess all you need further is the size of the struct.

    In either case, you would have to use a generic pointer type to get to the different members. In the simple case, a pointer to int or unsigned char would work. Unsigned char also works for the "using an offset" case.


    Edit: For your case, something like this would work
    Code:
    typedef struct 
    {
       int field[1];
    } generic;
    
    typedef struct {
    int c1;
    int c2;
    } t_struct1;
    
    typedef struct {
    int c1;
    int c2;
    int c3;
    } t_struct2;
    
    void DumpStruct(const void * s, size_t size) {
       generic *pg = s;
       for (int i = 0; i < size / sizeof(*pg); i++) {
        printf("Var %d = %d (%08x)\n", i, pg->field[i], pg->field[i]);
       }
    }
    Of course, you could dump the value as bytes, using further casts if you like.


    --
    Mats
    Last edited by matsp; 04-01-2009 at 04:24 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. How to access members of a struct
    By Bnchs in forum C Programming
    Replies: 9
    Last Post: 03-25-2008, 02:28 PM
  3. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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