Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Programming and Data Structure
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
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.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
CS352-Week 2. Topics Heap allocation References Pointers.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Pointers OVERVIEW.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Pointers *, &, array similarities, functions, sizeof.
Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
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.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Variables and memory addresses
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
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++
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
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.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
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?
Standard Version of Starting Out with C++, 4th Edition
Pointers and Pointer-Based Strings
Student Book An Introduction
Pointer.
Lecture 6 C++ Programming
Pointers and Pointer-Based Strings
Chapter 9: Pointers and String
Presentation transcript:

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming

Outlines 2 Memory and Variable Pointer and its operation Call by reference

Outcomes 3 Explain the relationship among memory address, identifier and variable. Declare pointer and performs related operation. Write function with call by reference features.

Syntax Summary 4 Punctuators *,& Constant NULL

Memory and Variable 5 Variable is used to store data that will be accessed by a program on execution. Normally, variable will be stored in the main memory A variable has four attributes: Value - the content of the variable Identifier - the name of the variable Address - the memory location of the variable Scope - the accessibility of the variable

Main Memory 6

Variable and Memory 7 void main(){ int x; int y; char c; x=100; y=200; c=`a`; } IdentifierValueAddress x10030 y20034 c‘a’38

Variable and Memory 8 Most of the time, the computer allocates adjacent memory locations for variables declared one after the other. A variable’s address is the first byte occupied by the variable. Address of a variable depends on the computer, and is usually in hexadecimal (base 16 with values 0-9 and A- F). e.g. 0x00023AF0, 00023AF0

Pointers 9 In C++, a pointer is a variable which designs to store the address of another variable. When a pointer store the address of a variable, we said the pointer is pointing to the variable. Pointer, like normal variable has a type, its type is determined by the type of variable it points to. Variable typeint x;float x;double x;char x; Pointer typeint*Pointx;float*Pointx;double*Pointx;char*Pointx;

* and & operator 10 To declare a pointer variable, place a “*” sign before an identifier name: char *cPtr;//a character pointer int *nPtr;//a integer pointer float *fp;//a floating point pointer To retrieve the address of a variable, use the “&” operator: int x; nPtr=&x; // &x will return the address of variable x; To access the variable a pointer pointing to, use “*” operator (dereference ) *nPtr=10;//x=10 y=*nPtr; //y=x

Example 11 int x,y;//x and y are integer variables void main(){ int *p1,*p2; /*p1 and p2 are pointers of integer typed*/ x=10; y=12; p1=&x; /* p1 stores the address of variable x*/ p2=&y; /* p2 stores the address of variable y*/ *p1=5; /* p1 value unchanged but x is updated to 5 */ *p2=*p1+10;/*what are the values of p2 and y?*/ }

Common operations 12 Set a pointer p1 point to a variable x p1=&x; Set a pointer p2 point to the variable pointed by another pointer p1 p2=p1 Update the value of variable pointed by a pointer *p2=10; Retrieve the value of variable pointed by a pointer int x=*p2;

More examples 13 int x=3; int y=5; int*p1=&x; int*p2=&y; y=*p1-*p2; p1=p2; y=*p1+*p2; x=p1+1; p2=p1+2; x=*p2**p1; x=*p2/*p1;///* y=x**p2; y+=*p1; *p2+=3; *p1*=*p2;

Guideline 14 * operator will give the value of pointing variable (so that you can directly update/modify the pointing variable) E.g., int x; int*p=&x; then using “*p” is equal to “x”; & operator will give the address of a variable (so that you can indirectly update/modify the pointing variable)

Applications of pointer 15 Call by Reference Fast Array Access Will be covered in later class Dynamic Memory Allocation Require additional memory space for storing value. Similar to variable declaration but the variable is stored outside the program.

Call by reference 16 Pass the address of a variable to a function call by value cannot be used to update arguments to function Consider the following function

Call by value 17 void f (char c1_in_f){ c1_in_f='B'; //c1_in_f=66 } void main(){ char c1_in_main='A'; // c1_in_main =65 f(c1_in_main); cout << c1_in_main; //print 'A' }

Call by value 18 When calling f(), the value of c1_in_main (65 or 'A') is copied to variable c1_in_f Inside f(), the value of c1_in_f is changed to 66 or 'B'. c1_in_main remains unchanged (65 or 'A'). In call by value, there is no way to modify c1_in_main inside f(), unless we use the return statement.

Call by value 19 void f (char c1_in_f){ c1_in_f='B'; //c1_in_f=66 return c1_in_f; } void main(){ char c1_in_main='A'; // c1_in_main =65 c1_in_main=f(c1_in_main); cout << c1_in_main; //print 'A' }

Call by reference void f (char *c1_ptr){ *c1_ptr='B'; } void main(){ char c1_in_main='A'; // c1_in_main =65 f(&c1_in_main); cout << c1_in_main; //print 'B' } 20 When f() is called, the following operation is performed c1_ptr = &c1_in_main; First, the value & c1_in_main (which is 3A8E) is retrieved

Call by reference void f (char *c1_ptr){ *c1_ptr='B'; } void main(){ char c1_in_main='A'; // c1_in_main =65 f(&c1_in_main); cout << c1_in_main; //print 'B' } VariableVariable typeMemory location Content c1_in_mainchar3A8E65 c1_ptrchar pointer40003A8E Location of c1_in_main (location 3A8E) is assigned to c1_ptr1 c1_ptr = &c1_in_main; Assign location 3A8E to c1_ptr points to

Call by reference void f (char *c1_ptr){ *c1_ptr='B'; } void main(){ char c1_in_main='A'; // c1_in_main =65 f(&c1_in_main); cout << c1_in_main; //print 'B' } VariableVariable typeMemory location Content c1_in_mainchar3A8E66 c1_ptrchar pointer40003A8E c1_ptr='B'; //error Reason: c1_ptr1 stores a location so it cannot store a char (or the ASCII code of a char) update c1_ptr points to location 3A8E (that is the variable c1_in_main). *c1_ptr refers to the variable pointed by c1_ptr, i.e. the variable stored at 3A8E

Note the different meaning of * void f (char *c1_ptr){ *c1_ptr='B'; } void main(){ char c1_in_main='A'; // c1_in_main =65 f(&c1_in_main); cout << c1_in_main; //print 'B' } 23 The type of c1_ptr is char* (pointer to star) dereference a pointer: c1_in_main=‘B’

Exercise: what are the errors? 24 int x=3; char c='a'; char *ptr; ptr=&x; ptr=c; ptr=&c;

Answer 25 int x=3; char c='a'; char *ptr; ptr=&x; //error: ptr can only points to a char, but not int ptr=c; //error: cannot assign a char to a pointer. A pointer can only store a location ptr=&c; //correct

Exercise: What is the output? 26 int num=100; int *ptr1; ptr1=&num; *ptr1=40; cout << num;

Answer 27 int num=100; int *ptr1; ptr1=&num; *ptr1=40; cout << num; //print 40

Call by value and call by reference 28 In call by value, only a single value can be returned using a return statement. In call by reference, the argument(s) can be a pointer which may reference or points to the variable(s) in the caller function. More than one variables can be updated, achieving the effect of returning multiple values.

Call-by-Value Pass value as parameter. int add(int m, int n) { int c; c = m + n; m = 10; return c; } void main() { int a=3, b=5,c; c=add(a,b); cout << a<<“ “<< c << endl; }

Pass address as parameter. Call-by-Reference int add(int *m, int *n) { int c; c = *m + *n; *m = 10; return c; } void main() { int a=3, b=5,c; c=add(&a,&b); cout << a<<“ “<< c << endl; }

Call-by-ValueCall-by-Reference Pass value as parameter. int add(int *m, int *n) { int c; c = *m + *n; *m = 10; return c; } void main() { int a=3, b=5,c; c=add(&a,&b); cout << a<<“ “<< c << endl; } Pass address as parameter. int add(int m, int n) { int c; c = m+n; m = 10; return c; } void main() { int a=3, b=5,c; c=add(a,b); cout << a<<“ “<< c << endl; }

Call by Reference - Guideline 32 Add the ‘*’ sign to the function parameters that store the variable call by reference. E.g. void cal(int *x, int y){ //x is call by reference //y is call by value …… …….*x; } Add the ‘&’ sign to the variable when it needs to be call by reference. E.g. cal(&result, factor);

Example: swapping values 33 #include using namespace std; void swap(int *p, int *q) { int tmp; tmp = *p;/* tmp = 3 */ *p = *q;/* *p = 7 */ *q = tmp;/* *q = 3 */ } int main(void) { int a = 3, b = 7; swap(&a, &b); cout << a <<“ “<< b <<endl; /* 7 3 is printed */ return 0; } 3 a p 7 b q tmp

Example: swapping values 34 #include using namespace std; void swap(int *p, int *q) { int tmp; tmp = *p;/* tmp = 3 */ *p = *q;/* *p = 7 */ *q = tmp;/* *q = 3 */ } int main(void) { int a = 3, b = 7; swap(&a, &b); cout << a <<“ “<< b <<endl; /* 7 3 is printed */ return 0; } 3 a p 7 b q 3 tmp

Example: swapping values 35 #include using namespace std; void swap(int *p, int *q) { int tmp; tmp = *p;/* tmp = 3 */ *p = *q;/* *p = 7 */ *q = tmp;/* *q = 3 */ } int main(void) { int a = 3, b = 7; swap(&a, &b); cout << a <<“ “<< b <<endl; /* 7 3 is printed */ return 0; } 7 a p 7 b q 3 tmp

Example: swapping values 36 #include using namespace std; void swap(int *p, int *q) { int tmp; tmp = *p;/* tmp = 3 */ *p = *q;/* *p = 7 */ *q = tmp;/* *q = 3 */ } int main(void) { int a = 3, b = 7; swap(&a, &b); cout << a <<“ “<< b <<endl; /* 7 3 is printed */ return 0; } 7 a p 3 b q 3 tmp

Example: swapping values (in C++) 37 #include using namespace std; void swap(int &p, int &q) { int tmp; tmp = p;/* tmp = 3 */ p = q;/* *p = 7 */ q = tmp;/* *q = 3 */ } int main(void) { int a = 3, b = 7; swap(a, b); cout << a <<“ “<< b <<endl; /* 7 3 is printed */ return 0; } 7 a p 3 b q 3 tmp

Parameters Passing: arrays 38 void f(int a[3]){ cout << a[0] <<endl; //1 is printed a[0]=10; } void main (void) { int a[3]={1,2,5}; //an array with 3 elements f(a); //calling f() with array a } The size of array is optional. void f(int a[]) Only need to input the array name!

Parameters Passing: arrays 39 void f(int*a){ cout << a[0] <<endl; //1 is printed a[0]=10; } void main (void) { int*a=new int[3]; //an dynamic array with 3 elements f(a); //calling f() with array a cout << a[0] <<endl; //10 is printed delete [] a; }

Parameters Passing: arrays 40 void f(int a[3]){ cout << a[0] <<endl; //1 is printed a[0]=10; } void main (void) { int a[3]={1,2,5}; //an array with 3 elements f(a); //calling f() with array a }

Summary 41 Pointer is a special variable used to store the memory address (location) of another variable. Pointer is typed, and its type is determined by the variable it pointing to. * operator has two meaning For declaration, e.g int *p1, char *pc; For dereference, e.g. x=*p1, *pc=‘b’; & operator return the address of any variable

Summary 42 Call by reference: pass the address of a variable to a function such that its content can be updated within the function. For a function returns one value only, we can use a return statement. For a function returns more than one value, one must use call by reference.