Download presentation
Presentation is loading. Please wait.
Published byElaine Barrett Modified over 9 years ago
1
CSC 107 – Programming For Science
2
Today’s Goal Discover best uses of structures in a program Using typedef to make meaningful names
3
Creating A struct
4
Using structs
5
Assigning struct s Like all variables, can assign struct s 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
6
Pointers to struct
7
typedef s 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;
8
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];
9
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];
10
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];
11
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];
12
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];
13
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];
14
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];
15
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];
16
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];
17
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];
18
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];
19
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];
20
Your Turn Get into your groups and try this assignment
21
For Next Lecture Spend 1 last day on struct s on Wednesday Write better solution to well-known problem Understand why struct s used in real-world problems Before moving on in class, solidify students knowledge Angel has Weekly Assignment #12 for Tuesday Last programming assignment Last programming assignment due next week
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.