3

Is there a way to loop through structs and assign a value to their members during the process?

I'm not really sure if I composed the question correctly, so I'll try showing it in code, that is of course invalid, but hopefully serves as better example:

struct example {
    int x;
    /* ... */
};

struct example s1;
struct example s2;

int *structs[] = {
    s1.x,
    s2.x
};

int main(void) {
    for (int i = 0; i < 2; i++) {
        *structs[i] = i;
    }

    return 0;
}

Basically, I need to automate the process of assigning values to multiple structures, but I don't know how. Is this even possible in C?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ieattacos9
  • 33
  • 4
  • 3
    Why not simply use an array? – 500 - Internal Server Error Jul 28 '19 at 19:47
  • 1
    Instead of asking about your solution to a (for us) unknown problem, please ask about the problem itself. What is the reason you came up with this solution? Right now this question is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Also please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jul 28 '19 at 19:51
  • @500-InternalServerError Wouldn't that be inefficient? I actually have large amount of structs with multiple members, I think using structs is easier, more efficient and less chaotic. – ieattacos9 Jul 28 '19 at 20:06
  • @Someprogrammerdude Well, I don't know how to make it more clear, I'm asking if it's possible to loop through multiple structs like you can loop through an array. The code I provided was only used as a dummy code to better explain this. – ieattacos9 Jul 28 '19 at 20:07
  • What is this for? What is the *original* problem you want to solve by "loop through multiple structs like you can loop through an array"? The natural solution seems to be an array of structures, like `struct example structs[X];`. If that simple solution isn't enough to solve your problem, then you're not telling us enough about the actual problem. And to rephrase, the problem isn't "how to loop through structures", but the problem that causes you to want to solve it with "loop through structures". – Some programmer dude Jul 28 '19 at 20:50
  • I'm sorry, how's the original problem relevant to this? I literally asked if it's possible to "loop through multiple structs". Yes, the 'natural solution' `struct example structs[X];` is literally what I was looking for, so I'd assume a natural answer to the question "Is there a way to loop through structs.. Is this even possible in C?" would be: " Yes, there is a way! Declare an array of structures! - `struct example structs[X];` , have a nice day! " – ieattacos9 Jul 28 '19 at 21:14
  • First of all it will give us *context*. Context is very important, as it will help us tell you what you *need* instead of what you currently want. And a solution without a problem will really just let us guess about ways to help you. And there might even be other more suitable solutions to the problem. So please take some time to read the link I gave before about the XY problem. – Some programmer dude Jul 29 '19 at 05:13
  • I see. I apologize and thank you for the links. – ieattacos9 Jul 29 '19 at 19:24

1 Answers1

3

If you fix a bunch of trivial syntax errors, you can come up with:

struct example
{
    int x;
    /* ... */
};

struct example s1;
struct example s2;

int *structs[] = { &s1.x, &s2.x };

int main(void)
{
    for (int i = 0; i < 2; i++)
    {
        *structs[i] = i;
    }

    return 0;
}

Alternatively, you could use an array of pointers to structures:

struct example
{
    int x;
    /* ... */
};

struct example s1;
struct example s2;

struct example *examples[] = { &s1, &s2 };
enum { NUM_EXAMPLES = sizeof(examples) / sizeof(examples[0]) };

int main(void)
{
    for (int i = 0; i < NUM_EXAMPLES; i++)
    {
        examples[i]->x = i;
        // ...
    }

    return 0;
}

Both compile — both work.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thank you so much!! Can you please explain the use of enum in this situation? – ieattacos9 Jul 28 '19 at 20:03
  • In part, see ["`static const`" vs "`#define`" vs "`enum`"](https://stackoverflow.com/questions/1674032/static-const-vs-define-vs-enum/1674459#1674459). The idiom used is 'sizeof(array) / sizeof(element of array)` which yields the number of items in the array — as long as you are referring to an actual array definition and not a function parameter declared as an array. So `enum { NUM_EXAMPLES = sizeof(examples) / sizeof(examples[0]) };` takes the size of the array `examples` in bytes and divides by the size of the first entry in the array, yielding the number of examples. – Jonathan Leffler Jul 28 '19 at 20:07