C++ How to Program, 8/e ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
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.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
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.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 8 Pointers C++ How to Program, 9/e
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
(continue) © 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 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
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)
 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.
 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.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
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.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
 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 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
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.
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.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Computer Skills2 for Scientific Colleges
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
Pointers and Pointer-Based Strings
Chapter 7 from “C How to Program"
Pointers and Pointer-Based Strings
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
7 C Pointers.
EENG212 ALGORITHMS & DATA STRUCTURES
Data Structures and Algorithms Introduction to Pointers
Lecture 2 Arrays & Pointers May 17, 2004
CISC181 Introduction to Computer Science Dr
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.

 A pointer contains the memory address of a variable that, in turn, contains a specific value.  In this sense, a variable name directly references a value, and a pointer indirectly references a value.  Referencing a value through a pointer is called indirection.  Diagrams typically represent a pointer as an arrow from the variable that contains an address to the variable located at that address in memory. © by Pearson Education, Inc. All Rights Reserved.

 The declaration  int count;  int *countPtr declares the variable countPtr to be of type int * (i.e. a pointer to an int value) and is read (right to left) “ countPtr is a pointer to int.” ◦ Variable count in the preceding declaration is declared to be an int, not a pointer to an int. ◦ Each variable being declared as a pointer must be preceded by an asterisk ( * ).  When * appears in a declaration, is not multiplication; rather, it indicates that the variable being declared is a pointer.  Pointers can be declared to point to objects of any data type. © by Pearson Education, Inc. All Rights Reserved.

 Pointers should be initialized either when they’re declared or in an assignment.  A pointer may be initialized to 0, NULL or an address of the corresponding type.  A pointer with the value 0 or NULL points to nothing and is known as a null pointer. ◦ NULL is equivalent to 0, but in C++, 0 is used by convention.  The value 0 is the only in-teger value that can be assigned directly to a pointer variable without first casting the integer to a pointer type.  Similar concept as indirect addressing mode in HLA © by Pearson Education, Inc. All Rights Reserved.

 The address operator ( & ) is a unary operator that obtains the memory address of its operand. ◦ Same operator used in HLA ◦ HLA also uses the command lea( )  Assuming the declarations  int y = 5; // declare variable y int *yPtr; // declare pointer variable yPtr the statement  yPtr = &y; // assign address of y to yPtr assigns the address of the variable y to pointer variable yPtr.  Figure 8.2 shows a representation of memory after this assignment. © by Pearson Education, Inc. All Rights Reserved.

 Figure 8.3 shows another pointer representation in memory with integer variable y stored at memory location and pointer variable yPtr stored at memory location  You have to be very careful when performing operations using pointer variables.  The * operator, commonly referred to as the indirection operator or dereferencing operator, returns a synonym for the object to which its pointer operand points. ◦ Called dereferencing a pointer  A dereferenced pointer may also be used on the left side of an assignment.  The & and * operators are inverses of one another. © by Pearson Education, Inc. All Rights Reserved.

 There are three ways in C++ to pass arguments to a function—pass-by-value, pass-by-reference with reference arguments and pass-by-reference with pointer arguments.  In this section, we explain pass-by-reference with pointer arguments.  Pointers, like references, can be used to modify one or more variables in the caller or to pass pointers to large data objects to avoid the overhead of passing the objects by value.  In C++, you can use pointers and the indirection operator ( * ) to accomplish pass-by-reference.  This is how is done in C-language as it only supports pass-by-value, but this way you can accomplish similar results. © by Pearson Education, Inc. All Rights Reserved.

 When calling a function with an argument that should be modified, the address of the argument is passed.  When the address of a variable is passed to a function, the indirection operator ( * ) can be used in the function to form a synonym for the name of the variable this in turn can be used to modify the variable’s value at that location in the caller’s memory.  Figure 8.6 and Fig. 8.7 present two versions of a function that cubes an integer— cubeByValue and cubeByReference. © by Pearson Education, Inc. All Rights Reserved.

 Figure 8.7 passes the variable number to function cubeByReference using pass-by-reference with a pointer argument—the address of number is passed to the function.  The function dereferences the pointer and cubes the value to which nPtr points. ◦ This directly changes the value of number in main.  Figures 8.8–8.9 analyze graphically the execution of the programs in Fig. 8.6 and Fig. 8.7, respectively. © by Pearson Education, Inc. All Rights Reserved.

 In the function header and in the prototype for a function that expects a one-dimensional array as an argument, pointer notation may be used.  The compiler does not differentiate between a function that receives a pointer and a function that receives a one- dimensional array. ◦ The function must “know” when it’s receiving an array or simply a single variable which is being passed by reference.  When the compiler encounters a function parameter for a one-dimensional array of the form int b[], the compiler converts the parameter to the pointer notation int *b. ◦ Both forms are interchangeable. © by Pearson Education, Inc. All Rights Reserved.

 Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions.  pointer arithmetic—certain arithmetic operations may be performed on pointers: ◦ increment ( ++ ) ◦ decremented ( -- ) ◦ an integer may be added to a pointer ( + or += ) ◦ an integer may be subtracted from a pointer ( - or -= ) ◦ one pointer may be subtracted from another of the same type © by Pearson Education, Inc. All Rights Reserved.

 Assume that array int v[5] has been declared and that its first element is at memory location ◦ Remember int takes 32 bits or 4 bytes.  Assume that pointer vPtr has been initialized to point to v[0] (i.e., the value of vPtr is 3000 ).  Figure 8.16 diagrams this situation for a machine with four-byte integers. © by Pearson Education, Inc. All Rights Reserved.

 In conventional arithmetic, the addition yields the value ◦ This is normally not the case with pointer arithmetic. ◦ When an integer is added to, or subtracted from, a pointer, the pointer is not simply incremented or decremented by that integer, but by that integer times the size of the object to which the pointer refers. ◦ The number of bytes depends on the object’s data type. ◦ In HLA, this corresponds to the Indexed Addressing Mode and Scaled Index Addressing Mode.  For cPtr the = 3008 © by Pearson Education, Inc. All Rights Reserved.

28

29

 Pointer variables pointing to the same array may be subtracted from one another.  For example, if vPtr contains the address 3000 and v2Ptr contains the address 3008, the statement  x = v2Ptr - vPtr;  Would assign to x the number of array elements from vPtr to v2Ptr —in this case, 2.  Pointer arithmetic is meaningless unless performed on a pointer that points to an array. © by Pearson Education, Inc. All Rights Reserved.

 A pointer can be assigned to another pointer if both pointers are of the same type. © by Pearson Education, Inc. All Rights Reserved.

 Pointers can be compared using equality and relational operators. ◦ Comparisons using relational operators are meaningless unless the pointers point to members of the same array. ◦ Pointer comparisons compare the addresses stored in the pointers.  A common use of pointer comparison is determining whether a pointer is 0 (i.e., the pointer is a null pointer—it does not point to anything). © by Pearson Education, Inc. All Rights Reserved.

 An array name can be thought of as a constant pointer.  Pointers can be used to do any operation involving array subscripting.  Assume the following declarations:  int b[ 5 ]; // create 5-element int array b int *bPtr; // create int pointer bPtr  Because the array name (without a subscript) is a (constant) pointer to the first element of the array, we can set bPtr to the address of the first element in array b with the statement  bPtr = b; // assign address of array b to bPtr  equivalent to  bPtr = &b[ 0 ]; // also assigns address of array b to bPtr © by Pearson Education, Inc. All Rights Reserved.

 Array element b[ 3 ] can alternatively be referenced with the pointer expression  *( bPtr + 3 )  The 3 in the preceding expression is the offset to the pointer.  This notation is referred to as pointer/offset notation. ◦ The parentheses are necessary, because the precedence of * is higher than that of +. © by Pearson Education, Inc. All Rights Reserved.

 Just as the array element can be referenced with a pointer expression, the address  &b[ 3 ]  can be written with the pointer expression  bPtr + 3  The array name can be treated as a pointer and used in pointer arithmetic.  For example, the expression *( b + 3 ) also refers to the array element b[ 3 ].  In general, all subscripted array expressions can be written with a pointer and an offset. © by Pearson Education, Inc. All Rights Reserved.

 Pointers can be subscripted exactly as arrays can.  For example, the expression  bPtr[ 1 ]  refers to the array element b[ 1 ] ; this expression uses pointer/subscript notation. © by Pearson Education, Inc. All Rights Reserved.