1 CSE1301 Computer Programming Lecture 16 Pointers 2.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
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.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 CSE1301 Computer Programming Lecture 16 Pointers.
Pointers Ethan Cerami Fundamentals of Computer New York University.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointers CSE 2451 Rong Shi.
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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Lecture 12: Pointers B Burlingame 25 Nov Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight.
POINTERS.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
Pointers *, &, array similarities, functions, sizeof.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
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.
Lecture 17: The Last Puzzle Piece with Functions.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
Week 12 Methods for passing actual parameters to formal parameters.
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
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.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Pointers What is the data type of pointer variables?
Computer Skills2 for Scientific Colleges
Introduction to Programming Using C
Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
EPSII 59:006 Spring 2004.
Lecture 9: Pointers B Burlingame 25 October 2017.
FIGURE 9-5 Integer Constants and Variables
Introduction to Programming
CSI-121 Structured Programming Language Lecture 16 Pointers 2
CSI-121 Structured Programming Language Lecture 16 Pointers
Programming fundamentals 2 Chapter 2:Pointer
Basic notes on pointers in C
Pointers and References
CSC 253 Lecture 8.
CSC 253 Lecture 8.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Computer Skills2 for Scientific Colleges
5.1 Introduction Pointers Powerful, but difficult to master
CS111 Computer Programming
Pointers Lecture 1 Thu, Jan 15, 2004.
C Programming Pointers
A simple function.
Pointers and dynamic objects
Pointers and References
Pointer Syntax Review *ptrName Declaring Pointers
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

1 CSE1301 Computer Programming Lecture 16 Pointers 2

2 Topics Review of pointer basics More on how to dereference a pointer More on function passing by reference More pointer examples

3 Recall A pointer points to another variable A pointer contains the address of another variable The * operator is used to declare a pointer –eg int* xPtr; The & operator gives you the address of a variable The * operator dereferences a pointer - gives you the value the pointer points to

4 Recall A pointer is declared to point to a specific data type Any data type can have a pointer pointing to it Like any other variable, the type of a pointer is fixed –So a variable that is declared to be a char* will always point to variables of type char

5 More on Dereferencing Pointers point to other variables We need to go through the pointer and find out the value of the variable it points to We do this by dereferencing the pointer, using the * operator But what is actually happening when we dereference?

6 Algorithm for Dereferencing To dereference a pointer, use *xPtr, which means: 1. Go to xPtr 2. Take the value you find there, and use it as an address 3. Go to that address 4. Return the value you find there

7 ’A’ 0x2000 ch: Pointer examples 0x2000 0x2004 chPtr: char ch; chPtr=&ch; char* chPtr=NULL; ch = ‘A’;

8 ’A’ 0x2000 ch Pointer examples 0x2001 char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr1 is ?? *cPtr2 is ?? cPtr1 is ?? cPtr2 is ?? ’B’ 0x2002 0x2003 init cPtr1 cPtr2 0x2000 0x2001

9 ’A’ 0x2000 ch Pointer examples 0x2001 char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr1 is ‘A’ *cPtr2 is ?? cPtr1 is ?? cPtr2 is ?? ’B’ 0x2002 0x2003 init cPtr1 cPtr2 0x2000 0x2001

10 ’A’ 0x2000 ch Pointer examples 0x2001 char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr1 is ‘A’ *cPtr2 is ‘B’ cPtr1 is ?? cPtr2 is ?? ’B’ 0x2002 0x2003 init cPtr1 cPtr2 0x2000 0x2001

11 ’A’ 0x2000 ch Pointer examples 0x2001 char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr1 is ‘A’ *cPtr2 is ‘B’ cPtr1 is 0x2000 cPtr2 is ?? ’B’ 0x2002 0x2003 init cPtr1 cPtr2 0x2000 0x2001

12 ’A’ 0x2000 ch Pointer examples 0x2001 char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr1 is ‘A’ *cPtr2 is ‘B’ cPtr1 is 0x2000 cPtr2 is 0x2001 ’B’ 0x2002 0x2003 init cPtr1 cPtr2 0x2000 0x2001

13 ’A’ 0x2000 ch Pointer examples 0x2001 char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init;... cPtr1=&init; *cPtr1 is ?? ’B’ 0x2002 0x2003 init cPtr1 cPtr2 0x2000 0x2001

14 Assigning to pointers Pointers are just normal variables, that happen to have the type “address of ” Pointers can be assigned to the address of any variable (of the right type) The value of a pointer can change, just like the value of any other variable The value of a pointer can be manipulated, just like the value of any other variable

15 ’A’ 0x2000 ch Pointer examples 0x2001 char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr2=‘.’; ’B’ 0x2002 0x2003 init cPtr1 cPtr2 0x2000 0x2001

16 ’A’ 0x2000 ch Pointer examples 0x2001 char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr2=‘.’; cPtr2 is ?? ’.’ 0x2002 0x2003 init cPtr1 cPtr2 0x2000 0x2001

17 ’A’ 0x2000 ch Pointer examples 0x2001 char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr2=‘.’; cPtr2 is 0x2001 (same as before - why would it change?) ’.’ 0x2002 0x2003 init cPtr1 cPtr2 0x2000 0x2001

18 Pointers as Parameters Recall that parameters are normally passed as copies f(x) takes a copy of the value of x and passes it to f This is called passing by value When you pass the address of a variable, you tell the function where to find the actual variable, not just a copy of it

19 Pointers as Parameters (cont) Passing the address means the function can go and look at the variable, and change its value if it wants to. This is called passing by reference If you pass by reference, you can change the variable If you pass by value, you can only change the copy - this has no effect on the original variable

20 Advantages of passing by reference Efficient –because you are not wasting space by making extra copies of variables every time you call a function Another way to return information from a function –How do you return more than one value from a function? Using parameters passed by reference!

21 Disadvantages of passing by reference Harder to keep track of where (and how) a variable changes –Now changes could happen anywhere in a program, not just in the function a variable was born in (is local to) Functions are less neat –a function that returns a single value is mathematically neat, one that changes other values is messier to define precisely

22 More pointer examples int i=0; int* myPtr=NULL; int x=3; myPtr=&x; *myPtr=34; /* set myPtr to point to x */ /* set x to be 34, using myPtr */ /* set myPtr to point to i */ /* print i using myPtr */ /* print the address of i */ myPtr=&i; printf(“%d”,*myPtr); printf(“%p”,myPtr);

23 More pointer examples float x=5.4,y=78.25; float* xPtr=NULL; float* yPtr=NULL; xPtr=&x; yPtr=&y; /* set xPtr to point to x */ /* set yPtr to point to y */ /* put the value of y in x using pointers */ /* put 45.0 in y using yPtr */ *xPtr=*yPtr; *yPtr=45.0