The question is usually: “I like to loop through the members of the following struct. How can I do that?” or “Can I loop through the data members of a struct?”. Well, if we think that a struct is stored -of course- in the memory and everything in there can be pointed. Remember function pointers for example. So, set a pointer at the struct and then just increment the pointer, in order to traverse the data members. Maybe some ideas have arrived at your head. Let’s see some code and we comment latter.

#include <stdio.h>

struct foo{
    int a1;
    int a2;
    int a3;
    int a4;
    int a5;
    int a6;
    int a7;
    int a8;
    int a9;
    int a10;

int main(void)
    struct foo test;
    test.a1 = 1;
    test.a2 = 2;
    test.a3 = 3;
    test.a4 = 4;
    test.a5 = 5;
    test.a6 = 6;
    test.a7 = 7;
    test.a8 = 8;
    test.a9 = 9;
    test.a10 = 10;

    int *p;
    p = (int*)&test;
    int i;
    for(i=0;i<10;i=i+1) {
        printf("a%d is %d\n",i+1,*(p+i));
    return 0;

This code comes from G. Samaras.

If we had an int, a long and a string how could we traverse through it? Well you should think in terms of bytes and do that, but you see that this may become not simple. Moreover, in case you have the data members, all of the same type, it is pretty obvious² that we should use an array in this case.

Notice also, that in C++, things can be a little more tricky… In Java we could just use reflection. Garbage collectors use a tactic like this (not this one, but a similar one..). Sure thing is that this code is not useful and was developed by me for fun, just to see if these questions had a yes answer.

Have questions about this code? Comments? Did you find a bug? Let me know! 😀
Page created by G. (George) Samaras (DIT)


5 thoughts on “loopStructs.c

  1. Pingback: looping struct

  2. Just be careful about alignment when you do this:

    C99 A pointer to an object or incomplete type may be converted to a pointer to a different
    object or incomplete type. If the resulting pointer is not correctly aligned
    57)for the pointed-to type, the behavior is undefined. Otherwise, when converted back again, the
    result shall compare equal to the original pointer. When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.

    A less boring structure would be harder to reflect. You might find yourself reduced to pointing to and processing the individual bytes of an object.

  3. Whiteflags thanks for your comment. I admit I am not totally sure of what you are saying -> I mean I can not thing of some code now that would expose this undefined behavior. However, I am can say that this code is not here for usage, but for demonstration and as answers to these questions. I do not suggest anybody to use it!
    Again, thank you for your comment. 🙂

    • If you want to know, you should look up memory alignment.

      If you try to access a double member with an int pointer this way it probably won’t work for instance. That’s what the standard is saying. That’s why I said a less boring structure would be harder to reflect. Most data structures are made up of differently typed data.

  4. Oh I see your point. Yes, I agree with you. That’s why I comment a less boring struct, that has an int, a long and a string after my code.. Thank you again for your comment 😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s