 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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,
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 (Pointers) Lecture 1 –Follow class notes –Some.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
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.
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. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: Pointers.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
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.
Lecture 17: The Last Puzzle Piece with Functions.
 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 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. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2006 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Review slides Mar 15, 2005.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Pointers Introduction CSCI 230
Chapter 7 - Pointers Outline 7.1 Introduction
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)
8 Pointers.
Zhen Jiang West Chester University
Pointers and Pointer-Based Strings
Pointers Kingdom of Saudi Arabia
Zhen Jiang West Chester University
5.1 Introduction Pointers Powerful, but difficult to master
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
CISC181 Introduction to Computer Science Dr
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Pointer of Complex Structure Other Pointer Operations Calling Functions by Reference Relationship between pointer and array Dynamic Data Storage

 2003 Prentice Hall, Inc. All rights reserved. 2 Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings

 2003 Prentice Hall, Inc. All rights reserved. 3 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 Pointer declarations –* indicates variable is pointer int *countPtr; declares pointer to int, pointer of type int * –Multiple pointers require multiple asterisks int *myPtr1, *myPtr2; count 7 countPtr count 7

 2003 Prentice Hall, Inc. All rights reserved. 4 Pointer Variable Declarations and Initialization Can declare pointers to any data type Pointer initialization –Initialized to 0, NULL, or address 0 or NULL points to nothing

 2003 Prentice Hall, Inc. All rights reserved. 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 yPtr y 5 yptr y 5 address of y is value of yptr

 2003 Prentice Hall, Inc. All rights reserved. 6 Pointer Operators * (indirection/dereferencing operator) –Returns synonym for object its pointer operand points to –*yPtr returns y (because yPtr points to y ). –dereferenced pointer is lvalue *yptr = 9; // assigns 9 to y * and & are inverses of each other Example 1 #include int main() { int *p; int q=3, k=5; p=&q; cout << "p is " <<p<<" *p is " <<*p<<" q is "<<q<<endl; *p=4; cout << "p is " <<p<<" *p is " <<*p<<" q is "<<q<<endl; *p=k; cout << "p is " <<p<<" *p is " <<*p<<" q is "<<q<<endl; return 0; }

 2003 Prentice Hall, Inc. All rights reserved. 7 Pointer Operators

 2003 Prentice Hall, Inc. All rights reserved. 8 Pointer of complex structure Example 2 #include struct Student{ int grade; char name[20]; }; int main() { struct Student *p, q; q.grade = 90; strcpy(q.name,"Smith"); cout << "Grade " <<q.grade<< " Name " <<q.name<<endl; p=&q; cout grade name<<endl; return 0; }

 2003 Prentice Hall, Inc. All rights reserved. 9 Pointer of complex structure

 2003 Prentice Hall, Inc. All rights reserved. 10 Other Pointer Operations Example 3 #include int main() { int i=3, j=3, *p1, *p2; p1=&i, p2=&j; if(*p1==*p2) //used to check if the content is the same. cout <<"*p1 Equal to *p2"<<endl; else cout<<"*p1 not Equal to *p2"<<endl; if(p1==p2) //used to check if they are pointing to the same unit. cout <<"p1 Equal to p2"<<endl; else cout <<"p1 not Equal to p2"<<endl; if(p1<=p2) //it has side-effect, do not use it. cout <<"p1 no more than p2"<<endl; else cout <<"p1 less than p2"<<endl; return 0; }

 2003 Prentice Hall, Inc. All rights reserved. 11 Other Pointer Operations

 2003 Prentice Hall, Inc. All rights reserved. 12 Calling Functions by Reference 3 ways to pass arguments to function –Pass-by-value arguments –Pass-by-reference with reference arguments –Pass-by-pointer arguments return can return one value from function Example 4

 2003 Prentice Hall, Inc. All rights reserved. 13 Calling Functions by Reference #include void f_point( int *i, int *j) { *i=3; *j=4; } void f_reference( int &i, int &j) { i=3; j=4; } void f_value (int i, int j) { i=3; j=4; } int main() { int p=1, q=2; f_point(&p,&q); cout << "p is "<< p << " q is " <<q <<endl; p=1; q=2; f_reference(p,q); cout << "p is "<< p << " q is " <<q <<endl; p=1; q=2; f_value(p,q); cout << "p is "<< p << " q is " <<q <<endl; return 0; }

 2003 Prentice Hall, Inc. All rights reserved. 14 Calling Functions by Reference

 2003 Prentice Hall, Inc. All rights reserved. 15 Relationship Between Pointers and Arrays Arrays and pointers closely related –Array name like constant pointer –Pointers can do array subscripting operations

 2003 Prentice Hall, Inc. All rights reserved. 16 Pointer variable and name of array c[6] Name of array (Note that all elements of this array starts from here, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c Pointer pointing to the address of c[0] Relationship Between Pointers and Arrays

 2003 Prentice Hall, Inc. All rights reserved. 17 c[6] Name of array (Note that all elements of this array starts from here, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Last element *(pointer+0), or *p *(pointer+7) Relationship Between Pointers and Arrays

 2003 Prentice Hall, Inc. All rights reserved. 18 Relationship Between Pointers and Arrays 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 ]

 2003 Prentice Hall, Inc. All rights reserved. 19 #include void f_point( char * i, char * j) { *(i+2)='E'; *(j+3)='E'; } void f_array( char i[], char j[]) { i[2]='E'; j[3]='E'; } int main() { char p[6]="hello", q[10]="howareyou"; f_point(p,q); cout << "p is "<< p << " q is " <<q <<endl; strcpy(p,"hello"); strcpy(q,"howareyou"); f_array(p,q); cout << "p is "<< p << " q is " <<q <<endl; return 0; } Relationship Between Pointers and Arrays Example 5

 2003 Prentice Hall, Inc. All rights reserved. 20 Dynamic Data Storage Dynamic memory management –Control allocation and deallocation of memory –Operators new and delete new operator –Create memory for object –Calls default constructor for object –Returns pointer of specified type –Format Providing initializers double *ptr = new double( ); Time *timePtr = new time( 12, 0, 0 ); Allocating arrays int *gradesArray = new int[ 10 ]; Example 6

 2003 Prentice Hall, Inc. All rights reserved. 21 Dynamic Data Storage delete –Destroy dynamically allocated object and free space –Operator delete Calls destructor for object Deallocates memory associated with object –Memory can be reused to allocate other objects –Deallocating arrays delete [] gradesArray; –First calls destructor for each object in array –Then deallocates memory delete time;

 2003 Prentice Hall, Inc. All rights reserved. 22 Dynamic Data Storage Example 6 #include int main() { char * p; int index; cout << "Input how many characters:" ; cin >>index; p = new char [index+1]; cin >> p; cout <<"p is: " <<p; delete [] p; p=NULL; return 0; }

 2003 Prentice Hall, Inc. All rights reserved. 23 Dynamic Data Storage

 2003 Prentice Hall, Inc. All rights reserved. 24 Dynamic Data Storage Example 7: Multiple-phase development –Step 1: 7_1.cc7_1.cc –Step 2: 7_2.cc7_2.cc –Step 3: 7_3.cc7_3.cc

 2003 Prentice Hall, Inc. All rights reserved. 25 Dynamic Data Storage 7_3.cc #include class MyArray{ public: MyArray(); MyArray(int); ~MyArray(); void printall(); void array_copy(MyArray&); private: int size; int *ptr; }; MyArray::MyArray() { size=0; ptr=NULL; } MyArray::MyArray(int t_size) { if(t_size>0){ size=t_size; ptr=new int[t_size+1]; cout << "Please input "<<t_size<< " intergers:"; for(int i=0;i<t_size;i++) cin >> ptr[i]; } else{ size=0; ptr=NULL; } MyArray::~MyArray() { size=0; delete [] ptr; }

 2003 Prentice Hall, Inc. All rights reserved. 26 Dynamic Data Storage 7_3.cc (continue) void MyArray::printall() { if(size==0) { cout <<"NULL"<<endl; return; } for(int i=0;i<size;i++) cout<<ptr[i]<<" "; cout <<endl; } void MyArray::array_copy(MyArray & b) { size=b.size; delete [] ptr; ptr=new int [size+1]; for(int i=0; i<size;i++) ptr[i]=b.ptr[i]; ptr[size]=0; } int main() { MyArray a, b(4); cout << "Before copying"<<endl; a.printall(); b.printall(); a.array_copy(b); cout << "After copying"<<endl; a.printall(); b.printall(); return 0; }

 2003 Prentice Hall, Inc. All rights reserved. 27 Dynamic Data Storage