Lecture 38: Typedefs
description
Transcript of Lecture 38: Typedefs
LECTURE 38:TYPEDEFS
CSC 107 – Programming For Science
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 #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};
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 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
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: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;
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];
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];
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];
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];
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];
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];
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];
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];
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];
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];
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];
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];
Your Turn
Get into your groups and try this assignment
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