Upload
cornelia-mcbride
View
212
Download
0
Tags:
Embed Size (px)
Citation preview
LECTURE 35:STRUCTURES
CSC 107 – Programming For Science
Today’s Goal
Learn what structures are & how they are used Why we need & why would use a structure What fields mean and how they are
declared How variables of struct type work and
interact
All About the Variables
Variable names location to store data Memory location's initial value is unknown Assignments update memory location with
new value Value at memory location used in place of
variable Type specified for each variable
declared Type defines exactly how variable can be
used Implicitly defines memory needed for
variable Specifies how the memory location set &
interpreted
Problem With Variables
What type can be used for this:
Problem With Variables
Is putting an int, double, or cString
Problem With Variables
What is the data type describing this action?
Problems With Variables
Sometimes need to group data Pixels: red, green, blue, & alpha
components Forces: mass & vector of acceleration Atomic Particles: position & charge & spin Students: name, assignments, programs, &
tests Would like to do this within single
variable But requires multiple values defined for
each variable Normally want to do this for many variables
What Is a struct?
Similar to real-world blueprint or schematic Outlines how to group items to make bigger
thing May/will have to end up making many
copies of item No change to others when single item
changed
Creating A struct
Declare struct outside of any function After #includes where normally find
declarations Each struct must be given name
Follows normal naming rules & conventions Within struct, must declare 1 or more
fields Name & data type required for each field Fields can be listed in any order Act like variables belonging to each copy of struct
Each struct variable gets own copy of fields
Declaring A Struct
struct hurricane { char name[10]; int year; int category;};
struct pixel { int red, blue, green; double red;};
struct student { char name[40]; // Need space for full name double year, gpa; // Okay, even with hurricane};
Declaring A Struct
struct hurricane { char name[10]; int year; int category;};
struct pixel { int red, blue, green; double red;};
struct student { char name[40]; // Need space for full name double year, gpa; // Okay, even with hurricane};
Declaring A Struct
struct hurricane { char name[10]; int year; int category;};
struct pixel { int red, blue, green; double red;};
struct student { char name[40]; // Need space for full name double year, gpa; // Okay, even with hurricane};
Declaring A Struct
struct hurricane { char name[10]; int year; int category;};
struct pixel { int red, blue, green; double red;};
struct student { char name[40]; // Need space for full name double year, gpa; // Okay, even with hurricane};
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
Using structs
Type of the variable is specific struct Variable gets own copy of all of the
fields Like variable or array entry, initial values
unknown No link between fields even if names are
same Even variables of same struct, fields not
linked
Variables of struct Type
struct color { int red, green, blue;};
int main(void) { struct color yellow = { 158, 154, 0 }; struct color purple; purple.red = 102; purple.green = 102; // Better way to initialize purple.blue = 152; cout << yellow.red << " != " << purple.red;
Using structs
Can assign struct variables to one another Variables must be of identical struct types Copies value of all fields but still remain
independent Locations will be same, since pointers &
arrays aliased In all other situation, must use fields
Cannot add, multiply, compare struct variables
For any legal use, individual fields always available
Arrays of structs can also be declared Within the array, each entry is struct
variable
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Is It Legal?
struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;
Ooohhh… tracing colors
struct color { int red, green, blue;};int main(void) { struct color paints[2]; struct color pantone; paints[0].red = 128; paints[0].blue = 0; paints[0].green = paints[0].blue * 2; paints[1].red = 0; paints[1].blue = paints[0].red; paints[1].green = (paints[0].green+paints[1].blue); paints[1].blue = 0; paints[0].red = 0; pantone = paints[1];
Your Turn
Get into your groups and try this assignment
For Next Lecture
Mix pointers & structs in 13.3 How do we get address of a struct for
pointer? Anything need to change to use field with
pointer? How can we create arrays of structs
dynamically?
Angel has Weekly Assignment #12 for tomorrow
Last programming assignment available now