Besides the other answers I'd like to add two more compact ways of creating instances. Example:
struct Person{
float position[2];
int age;
char name[20];
}
struct Person p1 = { {4, 1.1}, 20, "John" };
struct Person p2 = { .age=60, .name="Jane", .position={0, 0} };
printf("%s, aged %i, is located at (%f, %f)\n", p1.name, p1.age,p1.position[0], p1.position[1] );
printf("%s, aged %i, is located at (%f, %f)\n", p2.name, p2.age,p2.position[0], p2.position[1] );
output:
John, aged 20, is located at (4.000000, 1.100000)
Jane, aged 60, is located at (0.000000, 0.000000)
Note that for p1
the order of properties matches that of the struct definition. If you don't want to type struct all the time when you use a type you can define a new alias using
typedef struct Person SomeNewAlias;
SomeNewAlias p1;
and you can call the new alias the same as the old namejust fine
typedef struct Person Person;
Person p1;