Thinking about Structs
Using Structs
Structure Initialization
Combined Definition and Declaration
Struct Layout
GCC Struct Layout
Structs and Value Semantics
Structs and Pass-By-Value (Code Example)
Structs and Pass By Reference
Field via Pointer
Structs and Precedence
Returning Structs
Modifying Reference Parameters
Bad Ideas
Dynamically Allicating Structs
Literal Struct Values
Nesting Structures and Arrays
Arrays of Structs
Arrays of Structs
* You can store structures as array
elements.
struct Event schedule[] = {
{ “Wake up”, 6, 30 },
{ “Breakfast”, 7, 0 },
{ “OS Class”, 9, 35 },
{ .hour = 11, .minute = 0, .name = “Meeting” },
[ 5 ] = { “C Class”, 11, 45 }
};
* To get to an instance, we index into the
array
* To get to one of its fields, we use dot.
char *ename = schedule[2].name;
double etime = schedule[3].hour +
schedule[3].minute/60.0 ;
Arrays of Struct Instances
Arrays of Pointers to Structs
Nesting Structs(Different Structs)
Arrays of Pointers to Structs(fields)
Meet a New Friend, typedef
Why use typedef?