Newbie
Newbie

Reputation: 1613

How to set all struct members to same value?

I have a struct:

struct something {
    int a, b, c, d;
};

Is there some easy way to set all those a,b,c,d into some value without needing to type them separately:

something var = {-1,-1,-1,-1};

Theres still too much repetition (lets imagine the struct has 30 members...)

I've heard of "constructs" or something, but i want to set those values into something else in different part of the code.

Upvotes: 8

Views: 10857

Answers (6)

user6529631
user6529631

Reputation: 11

If you have

struct
{int a,b,c.......;}foo;

I've written that code and it seems to be working fine:

int* pfoo;
for (int i = 0; i < sizeof(foo); i++)
{
    pfoo = (int*)((int)(&foo) + i*sizeof(int));
    *pfoo = f(i/2); //set the values (here: the values of a random function f)
}

It writes the memory directly starting at &foo (the address of the first variable in the struct)

Upvotes: 1

pascal
pascal

Reputation: 3365

Make a union of your structure, and an array. And use a loop to initialize the array.

union something {
    struct {
       int a,b,c,d;
    };
    int init[4];
};

   something truc;
   for (int i=0; i<4; i++) truc.init[i] = -1;

Upvotes: 0

Alan
Alan

Reputation: 46883

You can define a method for the struct. So why not:

struct something {
    int a, b, c, d;

    void set_values(int val) 
    { 
      a = b = c = d = val;
    }
};

something foo;

foo.set_values(-1);

Its definitely worth mentioning the point @sbi raised in the comments: if you're intent is to initialize the struct, then you should do so with a constructor. You should avoid allowing users of your structs/object to put it in an unusable/error state.

Upvotes: 6

Georg Fritzsche
Georg Fritzsche

Reputation: 99074

As i gathered, you want your struct to stay a POD but still want to have some "convenience constructor".
Adding a constructor to it wouldn't work in this case as you'd lose the POD-ness, thus i'd use a helper function:

something make_something() {
    something s = { -1, -1, -1, -1};
    return s;
}

If you want to set it to varying values, let the function take a, maybe optional, parameter:

something make_something(int i = 0) {
     something s = { i, i, i, i };
     return s;
} 

Now you can get the definition and initialization down to one line:

something s = make_something(-1);

Upvotes: 1

Gunslinger47
Gunslinger47

Reputation: 7061

This is my second answer for this question. The first did as you asked, but as the other commentors pointed out, it's not the proper way to do things and can get you into trouble down the line if you're not careful. Instead, here's how to write some useful constructors for your struct:

struct something {
    int a, b, c, d;

    // This constructor does no initialization.
    something() { }

    // This constructor initializes the four variables individually.
    something(int a, int b, int c, int d) 
        : a(a), b(b), c(c), d(d) { }

    // This constructor initializes all four variables to the same value
    something(int i) : a(i), b(i), c(i), d(i) { }

//  // More concise, but more haphazard way of setting all fields to i.
//  something(int i) {
//      // This assumes that a-d are all of the same type and all in order
//      std::fill(&a, &d+1, i);
//  }

};

// uninitialized struct
something var1;

// individually set the values
something var2(1, 2, 3, 4);

// set all values to -1
something var3(-1);

Upvotes: 14

anon
anon

Reputation:

Simply give the struct a constructor:

struct something {
    int a, b, c, d;
    something() {
        a = b = c = d = -1;
    }
};

and then use it:

int main() {
   something s;    // all members will  be set to -1
}

you can also use the constructor to reset members:

int main() {
   something s;    // all members will  be set to -1
   s.a = 42;   
   s = something();  // reset everything back to -1
}

Upvotes: 9

Related Questions