CS157 Struct1
int
, float
, etc.) provided by the language.
struct
— a collection of named variables
union
— a type that can hold one of a set of variables
enum
— a set of named integers
typedef
— define a name for a user type
// Declare a person struct struct person { char name[50]; int age; char phone[15]; float height; };
struct
is a user-defined type that specifies a collection of
named variables of different data types.
{ }
, and end with ;
Field | Value | Type | Size |
---|---|---|---|
name | Jack Applin | char [50] | 50 bytes |
age | 58 | int | probably 4 bytes |
phone | 970-555-1212 | char [15] | 15 bytes |
height | 5.75 | float | probably 4 bytes |
Each field has its own memory location.
// Declare a person struct struct person { char name[50]; int age; char phone[15]; float height; };
// Declare a person struct struct person { char name[50]; int age; char phone[15]; float height; };
struct
keyword: struct structName variableName;
struct person fritz; struct person wim, darryl; struct person teachers[20];
struct
. It is
accessed by var.field where var is the struct
variable
and field is the field name.
// Declare a person struct struct person { char name[50]; int age; char phone[15]; float height; }; struct person liz; liz.age = 13; strcpy(liz.name, "Lizzie");
Work with variables the same way as regular variables of a particular data type.
Field | Value | Type |
---|---|---|
name | George W. Shrub | char [50] |
age | 103 | int |
height | 4.5 | float |
struct person { char name[50]; int age; float height; }; int main() { struct person who; strcpy(who.name, "George W. Shrub"); who.age = 103; who.height = 4.5; printf("%s is %d years old\n", who.name, who.age); return 0; }
Various ways to create a structure
// Defining both a new data type and variables: struct Date { int month, day; } birth, current; struct Date superbowl; birth.month = 11;
// Defining a new data type: struct Date { int month, day; }; struct Date now; now.month = 5;
// Defining variables, but no type: struct { int month, day; } birth, current; // Cannot make any new variables! birth.month = 11;
Initializer list (order is important!)
#include <stdio.h> struct Date { int month, day, year; // Crazy American order }; int main() { struct Date dad = {8, 1, 1922}; printf("Happy birthday, Dad: %02d/%02d/%d\n", dad.month, dad.day, dad.year); return 0; }
Happy birthday, Dad: 08/01/1922
Passing to functions
We can send structures to functions
#include <stdio.h> struct Date { int month, day; }; void modify(struct Date val) { val.month = 1; val.day = 28; } int main() { struct Date birth = {7, 4}; printf("Before: %02d/%02d\n", birth.month, birth.day); modify(birth); printf("After: %02d/%02d\n", birth.month, birth.day); return 0; }
Before: 07/04 After: 07/04
We can refer to globally defined structures
#include <stdio.h> struct Date { int month, day; } birth = {7, 4}; void modify() { birth.month = 1; birth.day = 28; } int main() { printf("Before: %02d/%02d\n", birth.month, birth.day); modify(); printf("After: %02d/%02d\n", birth.month, birth.day); return 0; }
Before: 07/04 After: 01/28
We can send a pointer to the structure
#include <stdio.h> struct Date { int month, day; }; void modify(struct Date *p) { p->month = 1; p->day = 28; } int main() { struct Date birth = {7, 4}; printf("Before: %02d/%02d\n", birth.month, birth.day); modify(&birth); printf("After: %02d/%02d\n", birth.month, birth.day); return 0; }
Before: 07/04 After: 01/28
Arrays
We can create arrays of structs just like any other type.
int main() { struct person people[3]; strcpy(people[0].name, "Jack"); people[0].height = 5.5 strcpy(people[2].name, "Pierre"); people[2].age = 54; ...etc... }
name | Jack | name | ? | name | Pierre |
age | ? | age | ? | age | 54 |
phone | ? | phone | ? | phone | ? |
height | 5.5 | height | ? | height | ? |
#include <stdio.h> #include <string.h> struct student { char name[50]; int age; }; #define NUM_STUDENTS 3 int main() { struct student cs157[NUM_STUDENTS] = { {"Bob", 84}, {"Rob", 14}, {"Zob", 4}, }; for (int i=0; i<NUM_STUDENTS; i++) printf("Student %d: %s %d years old\n", i+1, cs157[i].name, cs157[i].age); return 0; }
Student 1: Bob 84 years old Student 2: Rob 14 years old Student 3: Zob 4 years old
// Declare a union union int_or_float { // tag name int i; // one field float f; // another field };
// Declare a union union int_or_float { // tag name int i; // one field float f; // another field }; int main() { union int_or_float q; q.i = 14; q.f = 175.543; q.i = q.f; return 0; }
enum
type allows you to specify a finite
set of names to which C will automatically give values.
// Declare a enum enum traffic_light { // tag name red, // value yellow, // value green // value };
values
enum traffic_light { // tag name red, // value yellow, // value green // value }; void decide(enum traffic_light light) { switch (light) { case green: puts("go"); break; case red: puts("stop"); break; case yellow: puts("go very fast"); break; } }
enum
variables are passed by value, therefore the
function does not change the original
int
s, so the rules
for passing int
s apply.
type tag { ... } var1, var2, var3, ...;
struct
, union
, or enum
.