Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to edit multiple variables inside a structure in C

typedef struct
{
   string color;
   int age;
}Dog;


int main(void)
{
    Dog cookie = ("brown", 6);
    cookie.color = "blue";
    cookie.age = 12;
}

Hello, I’d like to know if there’s a way to edit multiple structure values as such,
while only using a single line instead of repeating "structName.varName" over and over.

Would there be a similar method for changing multiple values for objects in C as well? Thank you so much.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can assign multiple struct members a value at once, by using a compound literal temporary object:

cookie = (Dog) { "blue", 12 };

Or for increased readability, combine this with designated initializers (equivalent code):

cookie = (Dog) { .color = "blue", .age = 12 };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading