lecture 38: typedefs
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/1.jpg)
LECTURE 38:TYPEDEFS
CSC 107 – Programming For Science
![Page 2: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/2.jpg)
Today’s Goal
Discover best uses of structures in a program Using typedef to make meaningful names
![Page 3: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/3.jpg)
Creating A struct
Declare struct outside 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 types struct student { double gpa; char name[40]; // Arrays are okay int *year; // Pointers fine, also};
![Page 4: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/4.jpg)
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
![Page 5: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/5.jpg)
Assigning structs
Like all variables, can assign structs Identical struct type needed for 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
![Page 6: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/6.jpg)
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
![Page 7: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/7.jpg)
typedefs For Simplicity
Make synonyms for types using a typedef:typedef original 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
typedef long BIG_INTEGER;typedef unsigned long bob;typedef char * CSTRING;typedef struct energy ENERGY;
![Page 8: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/8.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 9: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/9.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 10: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/10.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 11: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/11.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 12: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/12.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 13: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/13.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 14: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/14.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 15: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/15.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 16: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/16.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 17: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/17.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 18: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/18.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 19: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/19.jpg)
Will It Compile?
typedef int MIDDLE;typedef char* CSTRING;typedef struct 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];
![Page 20: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/20.jpg)
Your Turn
Get into your groups and try this assignment
![Page 21: Lecture 38: Typedefs](https://reader036.vdocuments.net/reader036/viewer/2022062301/56815f01550346895dcdbeaf/html5/thumbnails/21.jpg)
For Next Lecture
Spend 1 last day on structs on 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