Download presentation
Presentation is loading. Please wait.
Published byOsborn Grant Modified over 9 years ago
1
CSC 107 - Programming for Science Lecture 36: Structures
2
Problem of the Day Perform the calculation below as fast as you can, and without pen, paper, or a calculator!): Take 1000 and add 40 Add 1000 Add 30 and then add 1000 Add 20 Add 1000 and then add 10 4100
3
Today’s Goal Should learn more about structs Passing structs to functions Returning structs from a function Making & using pointers to structs
4
Using structs Variable’s type is struct struct_name Variable’s fields act like a variable Access field using variable: variable.field Each variable contains every field Changing field in 1 variable does not it in others Reassigning field does not change variable’s other fields
5
Variables of struct Type struct force { double mass; int acceleration[3]; }; int main(void) { int i; struct force cueball, rock; cueball.mass = 30; rock.mass = cueball.mass * 500; rock.acceleration[2] = 0; cueball.acceleration[0] = 10; rock.acceleration[1] = 0; cueball.acceleration[1] = 10; cueball.acceleration[2] = 10; rock.acceleration[0] = 0; for (i = 0; i < 3; i++) { cueball.acceleration[i] = - cueball.acceleration[i] - 4; }
6
Using structs Can assign one struct variable to another Variables must be of identical struct types Otherwise, must use the fields Cannot add, multiply, compare, &c., structs Can pass variable’s fields to a function Just like any other variable or array element Can also pass struct variable to a function Just needs parameter’s type to match
7
Passing struct to a Function Like all arguments, must match type Parameter must be exactly the same struct Parameter gets copy of the variable Duplicates value of each & every field Fields’ values not changed during call Like passing anything but pointers & arrays Elements in an array field may change Just as when array is passed to function
8
Passing struct to Function struct energy { double potential, kinetic; }; void drop(struct energy dropMe) { dropMe.kinetic = dropMe.potential; dropMe.potential = 0; printf(“%lf %lf\n”, dropMe.kinetic, dropMe.potential); } int main(void) { struct energy ball; ball.kinetic = 0; ball.potential = 10 * 9.8 * 10; printf(“%lf %lf\n”, ball.kinetic, ball.potential); drop(ball); printf(“%lf %lf\n”, ball.kinetic, ball.potential);
9
Passing struct to Function struct person { char firstName[30], lastName[30]; }; void renameForProfHertz(struct person example) { strcpy(example.firstName, “Bob”); printf(“%s %s\n”, example.firstName, example.lastName); } int main(void) { struct person anon; strcpy(anon.firstName, “Jane”); strcpy(anon.lastName, “Doe”); printf(“%s %s\n”, anon.firstName, anon.lastName); renameForProfHertz(anon); printf(“%s %s\n”, anon.firstName, anon.lastName);
10
Returning struct From Function Functions can also return a struct Specifies the type of struct in its return type Needs a proper return statement Basically, works like any other return type Nothing really new here But good way to update a struct parameter
11
Passing struct to Function struct energy { double potential, kinetic; }; struct energy drop(struct energy dropMe) { dropMe.kinetic = dropMe.potential; dropMe.potential = 0; return dropMe; } int main(void) { struct energy ball, ball2; ball.kinetic = 0; ball.potential = 10 * 9.8 * 10; printf(“%lf %lf\n”, ball.kinetic, ball.potential); 0.0 980.0 ball2 = drop(ball); printf(“%lf %lf\n”, ball2.kinetic, ball2.potential); 980.0 0.0
12
Pointers to struct Can create pointer to a struct Assign using & to get address of struct Or use malloc() to dynamically allocate array Two ways to access pointer’s fields Use combination of * and. (*pointerToName).firstName Use a pointer (->) with your pointer pointerToName->firstName
13
Using Pointers to struct struct energy { double mass, potential, kinetic; }; int main(void) { int i; struct energy ball, *projectile; projectile = &ball; ball.mass = 100; ball.kinetic = 0; ball.potential = ball.mass * 9.8 * 10; for (i = 0; i kinetic = ball.mass * 9.8 * i; printf(“vel = %lf\n”, sqrt(ball.kinetic * 2 / ball.mass)); }
14
Your Turn Get back into groups and do activity
15
For Next Lecture Finish up with weekly assignments This week’s assignment should help for final
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.