Structure Summary
What is a structure:
· Data type you create with parts
· Can contain an array / Can be contained by an array
· Passes by value unless explicitly by pointer
Defining a struct:
·
struct workerstuff { int rate,
hours, gross;} ;
·
Use Typedef
o just the tag: typedef struct workerstuff Worker;
o the entire structure typedef struct { int rate, hours, gross;} Worker ;
Declaration
·
single struct variable
o option 1 with combined def
: struct workerstuff { int
rate, hours, gross;} worker;
o option 2 referencing earlier definition: struct workerstuff worker;
o If typedef used: Worker worker;
·
Array of
a structure
o struct workerstuff workerArr[];
·
Pointer
to a structure
o struct workerstuff * workerPtr;
Access variable:
·
single struct variable – dot : worker.rate;
·
Array of
a structure - Access via dot and subscript: WorkerArr[1].rate
·
Pointer
to a structure - Access via arrow : workerPtr->rate or (*workerPtr).rate
Union
·
Definition
by choices, not parts
·
Takes on
the size of the largest possible choice
·
Definition:
union number {int x; int * p;};
·
Declaration
and use have the same syntax as structure.
Enum:
·
Naming
constants, integer underlies it
·
Use it
to create a variable with only those possible choices
·
Definition:
enum days { SUN, MON, TUE, WED, THUR, FRI, SAT};
·
Declaration:
enum days day;
·
Use: day = SUN;
Good Tutorials:
• http://www.tutorialspoint.com/cprogramming/c_structures.htm
• http://denniskubes.com/2012/08/20/is-c-pass-by-value-or-reference/
• http://www.programiz.com/c-programming/c-enumeration