C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.

Slides:



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

Kernighan/Ritchie: Kelley/Pohl:
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
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.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: Pointers.
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.
Pointers Applications
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
Microsoft Visual C++.NET Chapter 61 Memory Management.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Pointers CSE 5100 Data Structures and Algorithms.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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 Pointers are a unique class of variables whose purpose is to “point” to other variables Pointers allow programmers direct access to memory addresses.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CNG 140 C Programming (Lecture set 10)
Pointers and References
Chapter 10: Pointers 1.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Objectives You should be able to describe: Addresses and Pointers
Pointers and References
Presentation transcript:

C++ for Engineers and Scientists Second Edition Chapter 12 Pointers

C++ for Engineers and Scientists, Second Edition2 Objectives Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming Errors

C++ for Engineers and Scientists, Second Edition3 Addresses and Pointers Address operator & placed in front of a variable provides the memory address of the variable

C++ for Engineers and Scientists, Second Edition4 Addresses and Pointers (continued) Pointer variable: used to store the address of another variable Indirection operator ( * ): when followed by a pointer, it means “the variable whose address is stored in” Example: numAddr = &num // numAddr contains address // of num *numAddr // numAddr points to num

C++ for Engineers and Scientists, Second Edition5 Addresses and Pointers (continued) To declare a pointer, specify the type of the variable to which it will point Syntax: dataType *pointerName; Example: int *numAddr;

C++ for Engineers and Scientists, Second Edition6 Addresses and Pointers (continued)

C++ for Engineers and Scientists, Second Edition7 Addresses and Pointers (continued) Declaration of a pointer includes the type of variable it points to; indicates how much storage to retrieve

C++ for Engineers and Scientists, Second Edition8 Addresses and Pointers (continued) References vs. pointers: –Reference is a named constant for an address and cannot be modified –Pointer is a variable and can be modified –References are said to be automatically dereferenced or implicitly dereferenced –Pointers must be explicitly dereferenced using the dereferencing operator *

C++ for Engineers and Scientists, Second Edition9 Addresses and Pointers (continued) To pass a simple variable as a function argument, use a reference When dynamic memory allocation is required, use pointers References: can be used as formal function parameters and function return types, and also as variables Reference variable: a new name for an existing variable (both point to the same location); also called an alias

C++ for Engineers and Scientists, Second Edition10 Addresses and Pointers (continued) Syntax: dataType& aliasName = variableName;

C++ for Engineers and Scientists, Second Edition11 Addresses and Pointers (continued) Original variable or its alias can be used Reference must be declared with same data type as original variable Reference cannot be equated with a constant Reference cannot be changed to point to another variable Multiple references can be declared on the same line; each must be preceded by &

C++ for Engineers and Scientists, Second Edition12 Addresses and Pointers (continued) Compiler automatically uses the variable when a new value is assigned to the reference Example: int b: // original variable int& a=b; // a is a reference variable // with b’s address a = 10; // change b’s value to 10 Compiler automatically performs an indirect access of b ’s value without explicitly using indirection symbol * ; called automatic dereference

C++ for Engineers and Scientists, Second Edition13 Addresses and Pointers (continued) Same example with a pointer: int b;// declare integer variable int *a = &b; // declare pointer a, // store b’s address *a = 10;// changes b’s value to 10 This example uses explicit dereference Pointer can be changed to point to another variable Indirection operator * must be used; also called dereferencing operator

C++ for Engineers and Scientists, Second Edition14 Array Names as Pointers Internally, the compiler uses addresses to access specific array elements Example: integer array grade To call grade [3], the compiler computes: &grade[3] = &grade[0] + (3 * sizeof(int))

C++ for Engineers and Scientists, Second Edition15 Array Names as Pointers (continued)

C++ for Engineers and Scientists, Second Edition16 Array Names as Pointers (continued) Array elements may be referenced in two ways:

C++ for Engineers and Scientists, Second Edition17 Array Names as Pointers (continued)

C++ for Engineers and Scientists, Second Edition18 Array Names as Pointers (continued) When an array is created, the array name becomes the name of a pointer constant containing the address of the first location in the array

C++ for Engineers and Scientists, Second Edition19 Array Names as Pointers (continued)

C++ for Engineers and Scientists, Second Edition20 Array Names as Pointers (continued) Array name and pointer can be used interchangeably in most respects, except: –True pointer is a variable, and its contents (an address) can be changed –Array name is a pointer constant, and its contents (an address) cannot be changed Array declared with size causes static allocation of memory for all the elements, whether used or not

C++ for Engineers and Scientists, Second Edition21 Array Names as Pointers (continued) Dynamic allocation allows the allocated memory to be determined and adjusted at run time, using new and delete operators Explicit dynamic requests are made in a declaration or assignment statement

C++ for Engineers and Scientists, Second Edition22 Array Names as Pointers (continued)

C++ for Engineers and Scientists, Second Edition23 Pointer Arithmetic Arithmetic operations can be done on pointer variables (as long as a valid address results) For arrays, the compiler will scale the arithmetic operation to ensure that the result points to a value of the correct type Addresses can be incremented or decremented using prefix and postfix increment and decrement operators

C++ for Engineers and Scientists, Second Edition24 Pointer Arithmetic (continued)

C++ for Engineers and Scientists, Second Edition25 Pointer Arithmetic (continued) Relational operators can be used to compare addresses in pointers

C++ for Engineers and Scientists, Second Edition26 Pointer Arithmetic (continued) Pointers can be initialized when declared If pointing to a variable, the variable must be declared first Pointers to arrays can be initialized when declared in two ways Example: double volts[5];// declare array double *zing = &volts[0];// first way double *zing = volts; // second way

C++ for Engineers and Scientists, Second Edition27 Passing Addresses Addresses can be implicitly passed to functions using reference variables Addresses can be explicitly passed to functions using pointers; this is a pass by reference Place the address operator & in front of the variable to pass its address Example: swap(&firstnum, &secnum)

C++ for Engineers and Scientists, Second Edition28 Passing Addresses (continued) Function must be created with pointer arguments Example: void swap(double *nm1Addr, double *nm2Addr); When an array is passed to a function, only its address is actually passed (an entire array is always passed by reference) The address of the array is also the address of element 0

C++ for Engineers and Scientists, Second Edition29 Passing Addresses (continued)

C++ for Engineers and Scientists, Second Edition30 Passing Addresses (continued)

C++ for Engineers and Scientists, Second Edition31 Passing Addresses (continued) Pointer notation can be used for multidimensional arrays, but it becomes complex as the array dimensions increase

C++ for Engineers and Scientists, Second Edition32 Passing Addresses (continued) An equivalent set of relationships:

C++ for Engineers and Scientists, Second Edition33 Passing Addresses (continued) Function name is also a pointer Pointer can be declared to point to a function name

C++ for Engineers and Scientists, Second Edition34 Common Programming Errors Attempting to store an address in a non-pointer variable Using a pointer to access non-existent array elements (out of bounds) Failing to use the bracket set [] after the delete operator when dynamically deallocating memory previously allocated by the new[] operator Incorrectly applying the address and indirection operators

C++ for Engineers and Scientists, Second Edition35 Common Programming Errors (continued) Taking the address of a pointer constant (which is already an address) Taking the address of a reference argument, reference variable, or register variable (which are already addresses) Initializing pointer variables incorrectly Failing to understand if a variable contains an address or is an address

C++ for Engineers and Scientists, Second Edition36Summary Address of a variable can be obtained using the address operator & Pointer variable stores the address of another variable Array name is a pointer constant Access to an array element using subscript notation can be replaced using pointer notation Arrays can be dynamically created at run time

C++ for Engineers and Scientists, Second Edition37 Summary (continued) Arrays are passed to functions as addresses When passing a single-dimensional array to a function, the function argument can be declared as a pointer or as an array Pointers can be incremented, decremented, compared, and assigned Values added to or subtracted from a pointer are automatically scaled based on the data type