1 / 21

Lecture 38: Typedefs

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 struct outside of any function After #include s where normally find declarations

dalila
Download Presentation

Lecture 38: Typedefs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 107 – Programming For Science Lecture 38:Typedefs

  2. Today’s Goal • Discover best uses of structures in a program • Using typedef to make meaningful names

  3. 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};

  4. 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

  5. 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

  6. 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

  7. 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;

  8. 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];

  9. 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];

  10. 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];

  11. 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];

  12. 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];

  13. 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];

  14. 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];

  15. 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];

  16. 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];

  17. 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];

  18. 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];

  19. 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];

  20. Your Turn • Get into your groups and try this assignment

  21. 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

More Related