210 likes | 306 Vues
Explore the power of typedefs in programming to simplify code and improve readability. Learn how to create structs, assign them, use pointers to structs, and address common compilation issues.
E N D
CSC 107 – Programming For Science Lecture 38:Typedefs
Today’s Goal • Discover best uses of structures in a program • Using typedef to make meaningful names
Creating A struct • Declare structoutside of any function • After #includes where normally find declarations • Within struct, must declare 1 or more fields • Name & data type required for each field • Fields can be listed in any order • Any type can be used for fields, even struct typesstruct student { double gpa; char name[40];// Arrays are okayint *year;// Pointers fine, also};
Using structs • Type of the variable is specific struct • Fields are like variable just as array entries are • Requires having actual variable to access • Each field is set & used independent of others • Code can access field as variableName.fieldName • No link between fields, even if from same struct
Assigning structs • Like all variables, can assign structs • Identical struct type neededfor source & target • Works like assigning fields in source to fields in target • Primitive values are copied like primitive variables • With assignment, arrays & pointers alias same value • Fields just like variables of the same type • Can be used just like any variable of that type • Need struct to get access to the field initially
Pointers to struct • Like all other types, can have pointer to struct • Assign using &to get address of struct variable • Can also dynamically allocate array using new • 2 ways to access fields with struct pointers • Follow pointer to struct & use the . operator(*pointerToStruct).fieldName • Use pointer operator, ->, to access field directlypointerToStruct->fieldName
typedefs For Simplicity • Make synonyms for types using a typedef:typedeforiginal type new_name; • Simplify & clarify code by making types meaningful • Synonym only for coder; computer ignores difference • Really creates shorthand; does NOT make new type typedeflongBIG_INTEGER;typedefunsigned long bob;typedefchar * CSTRING;typedefstruct energy ENERGY;
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Will It Compile? typedefint MIDDLE;typedef char* CSTRING;typedefstruct energy ENERGY;MIDDLE i = 5;CSTRING str = “Bob”;struct energy ball;ENERGY *nerf = &ball;int j = i;strcpy(str, “Yo, whatup dog”);nerf = new struct energy[100];i++;str = new CSTRING[23];nerf = new ENERGY[12];nerf[2] = ball;str = new char[42];
Your Turn • Get into your groups and try this assignment
For Next Lecture • Spend 1 last day on structson Monday • Write better solution to well-known problem • Understand why structs used in real-world problems • Before moving on in class, solidify students knowledge • Angel has Weekly Assignment #14 for Tuesday • Last programming assignment due in 1 week