-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-structs.c
41 lines (34 loc) · 833 Bytes
/
5-structs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
const int MAX_NAME_LENGTH = 16;
typedef struct Person
{
char name[MAX_NAME_LENGTH];
int age;
} Person;
void printPerson(Person p)
{
printf("%s (%d)\n", p.name, p.age);
}
int main(int argc, char *argv[])
{
Person p = {.name = "Ikke", .age = 42};
printPerson(p);
Person ducklings[] = {
{ .name = "Kwik", .age = 3},
{ .name = "Kwek", .age = 4},
{ .name = "Kwak", .age = 5}
};
for (int i = 0; i < sizeof(ducklings)/sizeof(Person); ++i) {
printPerson(ducklings[i]);
}
Person *firstDuck = &ducklings[0];
// Try some of the following and see what happens:
// firstDuck ++;
// firstDuck += 1;
for (int j = 0; j < MAX_NAME_LENGTH; ++j) {
firstDuck->name[j] = 'a';
}
for (int i = 0; i < sizeof(ducklings)/sizeof(Person); ++i) {
printPerson(ducklings[i]);
}
}