#include #include #include /* This declares a structure called Person */ struct Person { char name[30]; unsigned salary; unsigned age; }; /* This makes 'person_t' an alias for 'struct Person' */ typedef struct Person person_t; person_t *makePerson(char *nm, unsigned sal, unsigned a); void printPerson(person_t *pp); int main() { person_t *jdoep = makePerson("John Doe", 4321000u, 40u); person_t *arosep = makePerson("Alice Rose", 1732000u, 20u); /* This shows that stuctures can be assigned (copied). */ person_t jdoe = *jdoep; person_t sam; puts("Here are our people."); printPerson(jdoep); printPerson(arosep); puts("\nAnd 'jdoe' is"); printPerson(&jdoe); /* This shows how to access fields through a 'struct Person' variable. */ strcpy(sam.name, "Sam Rethwich"); sam.salary = 300000; sam.age = 32; puts("\nAnd 'sam' is"); printPerson(&sam); return 0; } /* * Allocates a 'struct Person' on the heap, initializes its fields, * and returns a pointer to it. * * This shows that a function can return a pointer to a structure. */ person_t *makePerson(char *nm, unsigned sal, unsigned a) { person_t *pp = (person_t *) malloc(sizeof(person_t)); strcpy(pp->name, nm); pp->salary = sal; pp->age = a; return pp; } /* * Prints the 'struct Person' object pointed by 'pp'. * * This shows that a pointer to a structure can be passed as an argument to a function. */ void printPerson(person_t *pp) { puts("Person"); printf("\tname: %s\n", pp->name); printf("\tsalary: %u\n", pp->salary); printf("\tage: %u\n", pp->age); }