CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.

Slides:



Advertisements
Similar presentations
1 Pointers and Strings Section 5.4, , Lecture 12.
Advertisements

C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
CSC Programming for Science Lecture 30: Pointers.
Encapsulation by Subprograms and Type Definitions
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CSC 107 – Programming For Science. Today’s Goal  Learn C functions to input and output data  Using printf to print out variables and strings  Read.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSC 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of.
Values, variables and types © Allan C. Milne v
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Today’s Material Aggregate Data Types: Structures and Unions
CS 261 – Data Structures Introduction to C Programming.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
CSC Programming for Science Lecture 36: 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.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  Using typedef to make meaningful names.
PHY 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
CSC Programming for Science Lecture 37 : Course Review.
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
CSC Programming for Science Lecture 27: Arrays as Parameters, Searching, & Sorting.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CSC Programming for Science Lecture 23: More on Function Parameters.
C++ Functions A bit of review (things we’ve covered so far)
Problem of the Day  Why are manhole covers round?
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Pointers and References
Functions Pass By Value Pass by Reference
Data Structures and Algorithms Introduction to Pointers
Java Programming Language
A simple function.
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Presentation transcript:

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