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.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Programming and Data Structure
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
CSC Programming for Science Lecture 30: Pointers.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
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?
What are Pointers? Different from other normal variables which can store values. pointers are special variables that can hold the address of a variable.
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.
Pointer Data Type and Pointer Variables
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.
CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
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.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Pointers Pointers are a unique class of variables whose purpose is to “point” to other variables Pointers allow programmers direct access to memory addresses.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 Workin’ with Pointas An exercise in destroying your computer.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
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 Programming for Science Lecture 36: Structures.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
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.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
CSC Programming for Science Lecture 34: Dynamic Pointers.
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CSC Programming for Science Lecture 26: Arrays.
Introduction to Programming Lesson 3. #include #include main ( ) { cout
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Problem of the Day  Why are manhole covers round?
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
CSC Programming for Science Lecture 5: Actual Programming.
Pointers & Arrays.
Student Book An Introduction
Basic notes on pointers in C
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
C Programming Pointers
Pointers & Arrays.
Pointers and dynamic objects
Presentation transcript:

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 are helpful & how pointers are used  WARNING: Pointers are hard  Ties together much of which we’ve done  Getting comfortable & happy with them takes a while

Variables  Variable  Variable name location to store data  Memory location's initial value is unknown  Assignments update memory location with new value  Memory location updated by assignment ONLY  When variable is used in program…  …uses current value at that memory location  Variable can store only one value  Use array otherwise so it can store 1 value per entry

Setting a Variable  Consider following code: int x, y; x = 5; y = x;

Setting a Variable  We understand this as: // Get memory locations for x & y int x, y; // Store 5 in x ’s memory location x = 5; // Get value in x ’s memory location and.. // copy it into y ’s memory location y = x;

x Also Known As…  Variable assignment only copies value  Updating x does not change y  Updating y does not change x  What if y used x ’s memory location?  x and y would then be aliases for same location  Changing x ’s value changes y ’s value  Changing y ’s value changes x ’s value

Pointers  Pointer  Pointer is another type of variable  Stores address as pointer’s value  With others, makes aliases for variables  Pointer variables are variables & must be declared  Like all declarations, must include type and name *  Add asterisk * before variable’s name  Once it is declared, can be used like any variable

Pointers  Pointer  Pointer is another type of variable  Stores address as pointer’s value  With others, makes aliases for variables  Pointer variables are variables & must be declared  Like all declarations, must include type and name *  Add asterisk * before variable’s name  Once it is declared, can be used like any variable  BUT address is value of this variable

Declaring an Pointer

Setting a Variable  We understand this as: // Get memory locations for x & y int x, y; // Store 5 in x ’s memory location x = 5; // Get value in x ’s memory location and.. // copy it into y ’s memory location y = x;

Setting a Pointer  Consider following code: int x, *y; x = 5; y = &x;

Setting a Pointer  We understand this as: // Get 2 memory locations named x & y int x, *y; // Store 5 in x ’s memory location x = 5; // Lookup x ’s memory location and… // store it's address in y 's location y = &x;

& and * Operators  & variable  & operator gets the address of a variable  Use with any variable, including array elements &  Using the & operator with anything else illegal  Others, like literals & equations, do not have address *  Follow pointer & get value at its address using *  Only use with pointers, since it could be multiply int x = 5 int *y = &x; int z = *y; float a, *b = &a, c = *b;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Coding With Pointers double x; double *y = &x; double *z = &(x+5); float a, *b; b = &a; y = x; y = &a; a = *b; b = &x; x = *b; x = *b + 5; a = *y;

Pointer Variables int x = 7 int *y = &x; int z = 10; cout << x << " " << y << " " << *y << " " << z << endl; *y = 6; cout << x << " " << y << " " << *y << " " << z << endl; x = 0; *y = *y + 3; cout << x << " " << y << " " << *y << " " << z << endl; y = &z; cout << x << " " << y << " " << *y << " " << z << endl; *y = 100; cout << x << " " << y << " " << *y << " " << z << endl; y = 100; cout << x << " " << y << " " << *y << " " << z << endl;

Your Turn  Get into your groups and try this assignment

For Next Lecture  Arrays & pointers discussed in Sections 12.6 & 12.8  Both use arrows in a trace; are they related?  How to exploit similarities in how each operates  Pointers & pointer arithmetic used with what types?  Angel has Weekly Assignment #10 posted today  Due next week as is the normal case