CSC 107 – Programming For Science
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
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
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
Creating A struct
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
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
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);
Pointers to struct
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];
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];
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];
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];
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];
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];
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];
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];
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];
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];
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];
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];
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];
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 =
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; }
Your Turn Get into your groups and try this assignment
For Next Lecture Using struct s in real programs in 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