Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reassign struct in C

Tags:

c

I'm new to C and experimenting with structs. After I've created a struct, is it possible to reassign it with curly brackets?

typedef struct {
    int height;
    int age;
} Person;

int main (void)
{
    Person bill = {100,35};
    bill = {120,34}; // error: expected expression before ‘{’ token
    bill = Person {120,34}; // error: expected expression before ‘Person’

    return 0;
}
like image 815
MachineElf Avatar asked Jan 16 '23 20:01

MachineElf


1 Answers

Not directly, but C99 has compound literals for that

bill = (Person){120,34};

you could even do things more readable by using designated initializers like

bill = (Person){ .height = 120, .age = 34, };
like image 51
Jens Gustedt Avatar answered Jan 25 '23 07:01

Jens Gustedt