1

I defined the following struct:

struct Color {
    unsigned int r, g, b, a;
};

Is there a way to cycle through its elements like:

Color c;

for (unsigned int i "in all struct elements") {
    c.i = 0;
}

So that, after that, all 4 unsigned int in c are set to 0?

7
  • Why don't you initialize those members in the struct? Commented Mar 4, 2020 at 11:59
  • 1
    Initialize at definition like Color c = {};? "Reset" using assignment as c = {};? Plain old setting bytes of memory as std::memset(&c, 0, sizeof c);? Commented Mar 4, 2020 at 12:01
  • @JVApen I don't need all variables (well, colors) of that type to be "transparent-white"! :) Commented Mar 4, 2020 at 12:06
  • @Someprogrammerdude thanks. Apologies... I'm a beginner and don't even have a book to follow. :\ I should. Commented Mar 4, 2020 at 12:06
  • 1
    Struct and class are the same thing, just different behavior regarding default access Commented Mar 4, 2020 at 16:10

1 Answer 1

1

Just use something like

Color color;
color = { 0 };
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.