Download presentation
Presentation is loading. Please wait.
Published bySolomon McCormick Modified over 8 years ago
1
CSC 107 – Programming For Science
2
Today’s Goal Discover best uses of structures in a program How we can mix pointers inside structures Assigning struct s & what this means for pointers Do struct s have an address? How can we get this? Pointers to struct s & operators who love them
3
Like, But Better Single value is enough to describe some data In real-life, however, this is not very common For everything else, there are struct s Groups values together using 1 or more fields Within struct, each field has name & type Variables are variables even if of struct type Each variable has own copy of every field in struct Copy values when assigning one struct to another
4
How Do struct s Work? These work just like a real-life blueprint Blueprint creates items with same layout (fields) Use as often (or as little) as want; cannot wear it out Making change to one item does not affect others
5
Creating A struct
6
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
7
Using struct s As Parameters Functions can use struct as a parameter Must pass same struct type for the argument Parameter assigned (copies) argument passed in Field values duplicated like all struct assignments Call will not & cannot change fields’ values Like passing any variable as argument to a function But, like normal, entries in array field may change
8
Functions Returning struct s Like all types, functions can return struct Return type specifies struct type to be returned Function SHOULD use return statements to end Works like any other return type we used so far Nothing to see here; world continues to rotate Easy updating struct parameter using this process struct energy drop(struct energy dropMe); // Lots of (pretend) code here for you to imagine struct energy bar, foo = { 34.5, 23.3 }; bar = drop(foo);
9
Pointers to struct
10
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
11
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
12
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
13
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
14
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
15
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
16
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; // Should be *ptr arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
17
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; // Should be *ptr arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
18
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; // Should be *ptr arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
19
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; // Should be *ptr arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
20
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; // Should be *ptr arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
21
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; // Should be *ptr arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; // Should be arr (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
22
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; // Should be *ptr arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; // Should be arr (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100];
23
Will It Compile? struct momentum { double mass, velX, velY; }; struct momentum rock = { 13, 1, 4 }; struct momentum projectile; struct momentum arr[10]; struct momentum *ptr = &rock; projectile = arr[4]; arr[2] = ptr; // Should be *ptr arr[5].mass = 7; (*ptr).velX = pow(arr[1].velY, 2); arr[5] = *ptr; arr[1].velX = ptr->mass; ptr = &arr; // Should be arr (&projectile)->velY = rock->velX + rock->velY; *ptr = new struct momentum[100]; // Should be ptr =
24
Tracing Pointers to struct struct energy { double mass, potent, kinet; }; struct energy ball; struct energy * projectile = &ball; ball.mass = 100; ball.kinet = 0; ball.potent = ball.mass * 10 * 10; for (int i = 0; i kinet = ball.mass * 10 * i; cout <<“v=” << sqrt(ball.kinet*2/ball.mass)<<endl; }
25
Your Turn Get into your groups and try this assignment
26
For Next Lecture Using struct s in real programs in 13.5-13.6 When would struct s be created in real program? How do we combine struct s for best use? How are typedef s used in the real world? Weekly Assignment #12 still out, but due in 8 days Last programming assignment Last programming assignment available in Angel
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.