1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.

Slides:



Advertisements
Similar presentations
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
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,
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
5.1Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings.
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Dale Roberts, Lecturer Computer Science, IUPUI.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Week 9 - Pointers.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 5 - Pointers and Strings
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
Chapter 5 - Pointers and Strings
Pointers and Pointer-Based Strings
Pointers Kingdom of Saudi Arabia
Chapter 5 - Pointers and Strings
Chapter 5 - Pointers and Strings
5.1 Introduction Pointers Powerful, but difficult to master
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
5.1 Introduction Pointers Powerful, but difficult to master
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 18 Pointers – Part II
Chapter 5 - Pointers and Strings
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
Presentation transcript:

1

Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2

Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally, variable contains specific value (direct reference) –Pointers contain address of variable that has specific value (indirect reference) Indirection –Referencing value through pointer 3 count 7 countPtr count 7

4 Pointer Declaration - * indicates variable is pointer * ; Examples int * myPtr; - declares pointer to int, pointer of type int * int * myPtr1, * myPtr2; - Multiple pointers require multiple asterisks int * Ptr1, Ptr2; int * myPtr, var ;

Pointer Initialization Can declare pointers to any data type Pointer initialization –Initialized to 0, NULL, or address 0 or NULL points to nothing Examples int * Ptr1=0, * Ptr2=NULL ; 5

Pointer Operators & (address operator) –Returns memory address of its operand –Example int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y –yPtr “points to” y 6 yPtr y 5 yptr y 5 address of y is value of yptr

Pointer Operators * (indirection/dereferencing operator) –Returns synonym for object its pointer operand points to –*yPtr returns y (because yPtr points to y ). *yptr = 9; // assigns 9 to y * and & are inverses of each other 7

Example // Using the & and * operators. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int a; // a is an integer 11 int *aPtr; // aPtr is a pointer to an integer a = 7; 14 aPtr = &a; // aPtr assigned address of a cout << "The address of a is " << &a 17 << "\nThe value of aPtr is " << aPtr; cout << "\n\nThe value of a is " << a 20 << "\nThe value of *aPtr is " << *aPtr; cout << "\n\nShowing that * and & are inverses of " 23 << "each other.\n&*aPtr = " << &*aPtr 24 << "\n*&aPtr = " << *&aPtr << endl; 25 * and & are inverses of each other

Example 9 26 return 0; // indicates successful termination } // end main The address of a is 0012FED4 The value of aPtr is 0012FED4 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 0012FED4 *&aPtr = 0012FED4 * and & are inverses; same result when both applied to aPtr

Calling Functions by Reference 10 3 ways to pass arguments to function –Pass-by-value –Pass-by-reference with reference arguments –Pass-by-reference with pointer arguments return can return one value from function Arguments passed to function using reference arguments –Modify original values of arguments –More than one value “returned”

Calling Functions by Reference Pass-by-reference with pointer arguments –Simulate pass-by-reference Use pointers and indirection operator –Pass address of argument using & operator –Arrays not passed with & because array name already pointer –* operator used as alias/nickname for variable inside of function 11

Example // Cube a variable using pass-by-value. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 int cubeByValue( int ); // prototype 9 10 int main() 11 { 12 int number = 5; cout << "The original value of number is " << number; // pass number by value to cubeByValue 17 number = cubeByValue( number ); cout << "\nThe new value of number is " << number << endl; return 0; // indicates successful termination } // end main 24 Pass number by value; result return ed by cubeByValue

Example // calculate and return cube of integer argument 26 int cubeByValue( int n ) 27 { 28 return n * n * n; // cube local variable n and return result } // end function cubeByValue The original value of number is 5 The new value of number is 125 cubeByValue receives parameter passed-by-value Cubes and return s local variable n

Example // Cube a variable using pass-by-reference 3 // with a pointer argument. 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 void cubeByReference( int * ); // prototype int main() 12 { 13 int number = 5; cout << "The original value of number is " << number; // pass address of number to cubeByReference 18 cubeByReference( &number ); cout << "\nThe new value of number is " << number << endl; return 0; // indicates successful termination } // end main 25 Apply address operator & to pass address of number to cubeByReference cubeByReferenc e modified variable number Prototype indicates parameter is pointer to int

15 26 // calculate cube of *nPtr; modifies variable number in main 27 void cubeByReference( int *nPtr ) 28 { 29 *nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr } // end function cubeByReference The original value of number is 5 The new value of number is 125 cubeByReference receives address of int variable, i.e., pointer to an int Modify and access int variable using indirection operator *

Pointer Expressions and Pointer Arithmetic Pointer arithmetic –Increment/decrement pointer (++ or -- ) –Add/subtract an integer to/from a pointer( + or +=, - or -= ) –Pointers may be subtracted from each other –Pointer arithmetic meaningless unless performed on pointer to array 5 element int array on a machine using 4 byte int s –vPtr points to first element v[ 0 ], which is at location 3000 vPtr = 3000 –vPtr += 2 ; sets vPtr to 3008 vPtr points to v[ 2 ] 16 pointer variable vPtr v[0]v[1]v[2]v[4]v[3] location

Pointer Expressions and Pointer Arithmetic Subtracting pointers –Returns number of elements between two addresses vPtr2 = v[ 2 ]; vPtr = v[ 0 ]; vPtr2 - vPtr == 2 Pointer assignment –Pointer can be assigned to another pointer if both of same type –If not same type, cast operator must be used 17

Pointer Expressions and Pointer Arithmetic Pointer comparison –Use equality and relational operators –Comparisons meaningless unless pointers point to members of same array –Compare addresses stored in pointers –Example: could show that one pointer points to higher numbered element of array than other pointer –Common use to determine whether pointer is 0 (does not point to anything) 18

Relationship Between Pointers and Arrays Arrays and pointers closely related –Array name like constant pointer –Pointers can do array subscripting operations Accessing array elements with pointers –Element b[ n ] can be accessed by *( bPtr + n ) Called pointer/offset notation –Addresses &b[ 3 ] same as bPtr + 3 –Array name can be treated as pointer b[ 3 ] same as *( b + 3 ) –Pointers can be subscripted (pointer/subscript notation) bPtr[ 3 ] same as b[ 3 ] 19

Example // Using subscripting and pointer notations with arrays. 3 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 int main() 10 { 11 int b[] = { 10, 20, 30, 40 }; 12 int *bPtr = b; // set bPtr to point to array b // output array b using array subscript notation 15 cout << "Array b printed with:\n" 16 << "Array subscript notation\n"; for ( int i = 0; i < 4; i++ ) 19 cout << "b[" << i << "] = " << b[ i ] << '\n'; // output array b using the array name and 22 // pointer/offset notation 23 cout << "\nPointer/offset notation where " 24 << "the pointer is the array name\n"; 25 Using array subscript notation.

Example for ( int offset1 = 0; offset1 < 4; offset1++ ) 27 cout << "*(b + " << offset1 << ") = " 28 << *( b + offset1 ) << '\n'; // output array b using bPtr and array subscript notation 31 cout << "\nPointer subscript notation\n"; for ( int j = 0; j < 4; j++ ) 34 cout << "bPtr[" << j << "] = " << bPtr[ j ] << '\n'; cout << "\nPointer/offset notation\n"; // output array b using bPtr and pointer/offset notation 39 for ( int offset2 = 0; offset2 < 4; offset2++ ) 40 cout << "*(bPtr + " << offset2 << ") = " 41 << *( bPtr + offset2 ) << '\n'; return 0; // indicates successful termination } // end main Using array name and pointer/offset notation. Using pointer subscript notation. Using bPtr and pointer/offset notation.

Example 22 Array b printed with: Array subscript notation b[0] = 10 b[1] = 20 b[2] = 30 b[3] = 40 Pointer/offset notation where the pointer is the array name *(b + 0) = 10 *(b + 1) = 20 *(b + 2) = 30 *(b + 3) = 40 Pointer subscript notation bPtr[0] = 10 bPtr[1] = 20 bPtr[2] = 30 bPtr[3] = 40 Pointer/offset notation *(bPtr + 0) = 10 *(bPtr + 1) = 20 *(bPtr + 2) = 30 *(bPtr + 3) = 40

23 Pointer-based strings  String as arrays of Characters char color[] = “Hello"; char color[] = { ‘H', ‘e', ‘l', ‘l', ‘o', '\0' };  String as pointer to character char *colorPtr = "blue"; ‘e’‘H’‘l‘l’‘o’ colorPtr ‘\0’ 5

Example // Copying a string using array notation 3 // and pointer notation. 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 void copy1( char *, const char * ); // prototype 10 void copy2( char *, const char * ); // prototype int main() 13 { 14 char string1[ 10 ]; 15 char *string2 = "Hello"; 16 char string3[ 10 ]; 17 char string4[] = "Good Bye"; copy1( string1, string2 ); 20 cout << "string1 = " << string1 << endl; copy2( string3, string4 ); 23 cout << "string3 = " << string3 << endl; return 0; // indicates successful termination

Example } // end main // copy s2 to s1 using array notation 30 void copy1( char *s1, const char *s2 ) 31 { 32 for ( int i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) 33 ; // do nothing in body } // end function copy // copy s2 to s1 using pointer notation 38 void copy2( char *s1, const char *s2 ) 39 { 40 for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) 41 ; // do nothing in body } // end function copy2 string1 = Hello string3 = Good Bye Use array subscript notation to copy string in s2 to character array s1. Use pointer notation to copy string in s2 to character array in s1. Increment both pointers to point to next elements in corresponding arrays.

Arrays of Pointers Arrays can contain pointers –Commonly used to store array of strings char *suit[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades" }; –Each element of suit points to char * (a string) –Array does not store strings, only pointers to strings –suit array has fixed size, but strings can be of any size 26 suit[3] suit[2] suit[1] suit[0]’H’’e’’a’’r’’t’’s’ ’\0’ ’D’’i’’a’’m’’o’’n’’d’’s’ ’\0’ ’C’’l’’u’’b’’s’ ’\0’ ’S’’p’’a’’d’’e’’s’ ’\0’