C++ Programming Lecture 17 Pointers – Part I

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
Advertisements

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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 (Pointers) Lecture 1 –Follow class notes –Some.
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.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
 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.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
 2007 Pearson Education, Inc. All rights reserved. 1 C Pointers Chapter 7 from “C How to Program" Another ref:
 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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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 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 2/2/05CS250 Introduction to Computer Science II 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.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 12 Functions – Part IV
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Week 9 - Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers and Pointer-Based Strings
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
Chapter 7 from “C How to Program"
C++ Programming Lecture 15 Arrays – Part II
Pointers and Pointer-Based Strings
C++ Programming Lecture 16 Arrays – Part III
Pointers Call-by-Reference CSCI 230
Pointers Kingdom of Saudi Arabia
Chapter 5 - Pointers and Strings
5.1 Introduction Pointers Powerful, but difficult to master
7 C Pointers.
EENG212 ALGORITHMS & DATA STRUCTURES
Pointers and Pointer-Based Strings
C++ Programming Lecture 18 Pointers – Part II
C++ Programming Lecture 20 Strings
CISC181 Introduction to Computer Science Dr
C Pointers Another ref:
Presentation transcript:

C++ Programming Lecture 17 Pointers – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

The Hashemite University Outline Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. The Hashemite University

The Hashemite University Introduction Pointers Powerful, but difficult to master. Simulate call-by-reference. Allow the creation of dynamic data structures that shrink and grow in size during run time, such as stacks, link lists, etc. Close relationship with arrays and strings. The Hashemite University

Pointer Variable Declarations and Initialization I Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain the address of a variable that has a specific value (indirect reference) Indirection Referencing a pointer value (accessing the contents of the memory location indicated by the address found inside the pointer). Pointer declarations * indicates that a variable is a pointer int *myPtr; declares a pointer to an int, a pointer of type int * Multiple pointers require multiple asterisks int *myPtr1, *myPtr2; The Hashemite University

Pointer Variable Declarations and Initialization II Can declare pointers to any data type Pointers initialization Initialized to 0, NULL, or an address 0 or NULL points to nothing The Hashemite University

The Hashemite University Pointer Operators I & (address operator) Returns the address of its operand It is different from the reference operator used in declaring reference variables. Can be applied to variables only (cannot be applied to expressions or constants  syntax error) Example int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y yPtr “points to” y yPtr y 5 yptr 500000 600000 address of y is value of yptr The Hashemite University

The Hashemite University Pointer Operators II * (indirection/dereferencing operator) Returns the value of what its operand points to *yPtr returns y (because yPtr points to y). * can be used to assign a value to a location in memory *yptr = 7; // changes y to 7 Dereferenced pointer (operand of *) must be an lvalue (no constants) * and & are inverses Cancel each other out *&myVar == myVar and &*yPtr == yPtr The Hashemite University

The Hashemite University Pointer Operators III Dereferencing a variable that is not a pointer is a syntax error, e.g.: int y = 10; cout << *y; //syntax error Dereferencing a null-pointer is a fatal error (i.e. you program will abort abnormally). The content of a pointer (which is a memory address) is expressed in hexadecimal when you print it on the screen. Some compilers may print it in decimal (machine dependent). You can change the address within the pointer during the program (i.e. you can change the variable to which the pointer is pointing). You must initialize the pointer with the address of a variable with the same data types as the pointer. e.g.: int *p; double y = 10; P = &y; //Syntax error The Hashemite University

The Hashemite University 1 // Fig. 5.4: fig05_04.cpp 2 // Using the & and * operators 3 #include <iostream> 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 12 13 a = 7; 14 aPtr = &a; // aPtr set to address of a 15 16 cout << "The address of a is " << &a 17 << "\nThe value of aPtr is " << aPtr; 18 19 cout << "\n\nThe value of a is " << a 20 << "\nThe value of *aPtr is " << *aPtr; 21 22 cout << "\n\nShowing that * and & are inverses of " 23 << "each other.\n&*aPtr = " << &*aPtr 24 << "\n*&aPtr = " << *&aPtr << endl; 25 return 0; 26 } The address of a is the value of aPtr. The * operator returns an alias to what its operand points to. aPtr points to a, so *aPtr returns a. Notice how * and & are inverses The address of a is 006AFDF4 The value of aPtr is 006AFDF4 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 006AFDF4 *&aPtr = 006AFDF4 The Hashemite University

Calling Functions by Reference Call by reference with pointer arguments Pass address of argument using & operator Allows you to change actual location in memory Arrays are not passed with & because the array name is already a pointer contains the address of the first element of the array in the memory. * operator used as alias/nickname for variable inside of function void doubleNum( int *number ) { *number = 2 * ( *number ); } *number used as nickname for the variable passed in When the function is called, must be passed an address doubleNum( &myNum ); The Hashemite University

The Hashemite University 1 // Fig. 5.7: fig05_07.cpp 2 // Cube a variable using call-by-reference 3 // with a pointer argument 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 void cubeByReference( int * ); // prototype 10 11 int main() 12 { 13 int number = 5; 14 15 cout << "The original value of number is " << number; 16 cubeByReference( &number ); 17 cout << "\nThe new value of number is " << number << endl; 18 return 0; 19 } 20 21 void cubeByReference( int *nPtr ) 22 { 23 *nPtr = *nPtr * *nPtr * *nPtr; // cube number in main 24 } Notice how the address of number is given - cubeByReference expects a pointer (an address of a variable). Inside cubeByReference, *nPtr is used (*nPtr is number). The original value of number is 5 The new value of number is 125 The Hashemite University

Using the const Qualifier with Pointers I Variable cannot be changed. const used when function does not need to change a variable. Attempting to change a const variable is a syntax error. const pointers: Point to same memory location during the whole program, i.e. the pointer cannot be modified to point to other data. Must be initialized when declared int *const myPtr = &x; Pointer points to const data: Means that the data cannot be modified indirectly (i.e. through the pointer) by dereferencing the pointer. The Hashemite University

Using the const Qualifier with Pointers II Types of const pointers: Non-constant pointer to a non-constant data e.g.: int *myPtr = &x; Constant pointer to a non-constant data: default for array names e.g.: int *const myPtr = &x; Non-constant pointer to a constant data e.g.: const int *myPtr = &x; Constant pointer to a constant data e.g.: const int *const myPtr = &x; The Hashemite University

The Hashemite University 1 // Fig. 5.13: fig05_13.cpp 2 // Attempting to modify a constant pointer to 3 // non-constant data 4 #include <iostream> 5 6 int main() 7 { 8 int x, y; 9 10 int * const ptr = &x; // ptr is a constant pointer to an 11 // integer. An integer can be modified 12 // through ptr, but ptr always points 13 // to the same memory location. 14 *ptr = 7; 15 ptr = &y; 16 17 return 0; 18 } Changing *ptr is allowed - x is not a constant. Changing ptr is an error - ptr is a constant pointer. Error E2024 Fig05_13.cpp 15: Cannot modify a const object in function main() The Hashemite University

The Hashemite University Additional Notes This lecture covers the following material from the textbook: Chapter 5: Sections 5.1 – 5.5 The Hashemite University