CSI-121 Structured Programming Language 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.
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.
1 CSE1301 Computer Programming Lecture 16 Pointers 2.
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 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.
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.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
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.
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
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
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
Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 10-1: Structure.
EPSII 59:006 Spring 2004.
Lecture 9: Pointers B Burlingame 25 October 2017.
Visit for more Learning Resources
Introduction to Programming
Lecture 6 C++ Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Programming fundamentals 2 Chapter 2:Pointer
Basic notes on pointers in C
Tejalal Choudhary “C Programming from Scratch” Pointers
Pointers and References
References and Pointers
Buy book Online -
CSC 253 Lecture 8.
CSC 253 Lecture 8.
CSI 121 Structured Programming Language Lecture 21 Character Strings
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Computer Skills2 for Scientific Colleges
5.1 Introduction Pointers Powerful, but difficult to master
Pointers Lecture 1 Thu, Jan 15, 2004.
C++ Pointers and Strings
C Programming Pointers
Chapter 9: Pointers and String
A simple function.
Pointers and dynamic objects
Pointers and References
Style Helpful variable and function names Bad style from HW 3b
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
Pointer Syntax Review *ptrName Declaring Pointers
Programming fundamentals 2 Chapter 3:Pointer
C Pointers Another ref:
Presentation transcript:

CSI-121 Structured Programming Language Lecture 16 Pointers 2 CSE1301 Sem 2 - 2004 CSI-121 Structured Programming Language Lecture 16 Pointers 2 Lecture 17: Pointers 2

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

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

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

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?

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

’A’ 0x2000 Pointer examples ch: chPtr: 0x2000 0x2004 char ch; CSE1301 Sem 2 - 2004 Pointer examples ’A’ 0x2000 ch: char ch; ch = ‘A’; 0x2000 0x2004 chPtr: char* chPtr=NULL; chPtr=&ch; Lecture 17: Pointers 2

’A’ ’B’ 0x2000 0x2001 Pointer examples ch init cPtr1 cPtr2 0x2000 CSE1301 Sem 2 - 2004 Pointer examples char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr1 is ?? *cPtr2 is ?? cPtr1 is ?? cPtr2 is ?? ’A’ ch 0x2000 ’B’ 0x2001 init 0x2002 0x2000 cPtr1 0x2001 cPtr2 0x2003 Lecture 17: Pointers 2

’A’ ’B’ 0x2000 0x2001 Pointer examples ch init cPtr1 cPtr2 0x2000 CSE1301 Sem 2 - 2004 Pointer examples 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 ?? ’A’ ch 0x2000 ’B’ 0x2001 init 0x2002 0x2000 cPtr1 0x2001 cPtr2 0x2003 Lecture 17: Pointers 2

’A’ ’B’ 0x2000 0x2001 Pointer examples ch init cPtr1 cPtr2 0x2000 CSE1301 Sem 2 - 2004 Pointer examples 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 ?? ’A’ ch 0x2000 ’B’ 0x2001 init 0x2002 0x2000 cPtr1 0x2001 cPtr2 0x2003 Lecture 17: Pointers 2

’A’ ’B’ 0x2000 0x2001 Pointer examples ch init cPtr1 cPtr2 0x2000 CSE1301 Sem 2 - 2004 Pointer examples 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 ?? ’A’ ch 0x2000 ’B’ 0x2001 init 0x2002 0x2000 cPtr1 0x2001 cPtr2 0x2003 Lecture 17: Pointers 2

’A’ ’B’ 0x2000 0x2001 Pointer examples ch init cPtr1 cPtr2 0x2000 CSE1301 Sem 2 - 2004 Pointer examples 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 ’A’ ch 0x2000 ’B’ 0x2001 init 0x2002 0x2000 cPtr1 0x2001 cPtr2 0x2003 Lecture 17: Pointers 2

’A’ ’B’ 0x2000 0x2001 Pointer examples ch init cPtr1 cPtr2 0x2000 CSE1301 Sem 2 - 2004 Pointer examples char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; ... cPtr1=&init; *cPtr1 is ?? ’A’ ch 0x2000 ’B’ 0x2001 init 0x2002 0x2000 cPtr1 0x2001 cPtr2 0x2003 Lecture 17: Pointers 2

Assigning to pointers Pointers are just normal variables, that happen to have the type “address of <some type>” 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

’A’ ’B’ 0x2000 0x2001 Pointer examples ch init cPtr1 cPtr2 0x2000 CSE1301 Sem 2 - 2004 Pointer examples ’A’ char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr2=‘.’; ch 0x2000 ’B’ 0x2001 init 0x2002 0x2000 cPtr1 0x2001 cPtr2 0x2003 Lecture 17: Pointers 2

’A’ ’.’ 0x2000 0x2001 Pointer examples ch init cPtr1 cPtr2 0x2000 CSE1301 Sem 2 - 2004 Pointer examples char ch=‘A’; char init=‘B’; char* cPtr1=NULL; char* cPtr2=NULL; cPtr1=&ch; cPtr2=&init; *cPtr2=‘.’; cPtr2 is ?? ’A’ ch 0x2000 ’.’ 0x2001 init 0x2002 0x2000 cPtr1 0x2001 cPtr2 0x2003 Lecture 17: Pointers 2

’A’ ’.’ 0x2000 0x2001 Pointer examples ch init cPtr1 cPtr2 0x2000 CSE1301 Sem 2 - 2004 Pointer examples 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?) ’A’ ch 0x2000 ’.’ 0x2001 init 0x2002 0x2000 cPtr1 0x2001 cPtr2 0x2003 Lecture 17: Pointers 2

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

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

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!

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

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

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