Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Starting Out with C++, 3 rd Edition 1 Chapter 9 – Pointers.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Lesson 9 Pointers CS 1 Lesson 9 -- John Cole1. Pointers in Wonderland The name of the song is called ‘Haddock’s Eyes’.” “Oh, that’s the name of the song,
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:
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
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.
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.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
1 Workin’ with Pointas An exercise in destroying your computer.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers Data Structures CSI 312 CSI Dept. Dr. Yousef Qawqzeh.
Pointers and Dynamic Arrays
Standard Version of Starting Out with C++, 4th Edition
Pointers & Arrays.
Chapter 9: Pointers.
Lecture 9 Files, Pointers
Chapter 10: Pointers Starting Out with C++ Early Objects
Pointer.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 10: Pointers Starting Out with C++ Early Objects
Chapter 9: Pointers.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 9: Pointers.
Pointers & Arrays.
Standard Version of Starting Out with C++, 4th Edition
Dynamic Memory Allocation
Pointers.
Presentation transcript:

Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers

Copyright © 2012 Pearson Education, Inc. Pointer Variables Pointer variable : Often just called a pointer, it's a variable that holds an address Because a pointer variable holds the address of another piece of data, it "points" to the data

Copyright © 2012 Pearson Education, Inc. Something Like Pointers: Arrays We have already worked with something similar to pointers, when we learned to pass arrays as arguments to functions. For example, suppose we use this statement to pass the array numbers to the showValues function: showValues(numbers, SIZE);

Copyright © 2012 Pearson Education, Inc. Something Like Pointers : Arrays The values parameter, in the showValues function, points to the numbers array. C++ automatically stores the address of numbers in the values parameter.

Copyright © 2012 Pearson Education, Inc. Something Like Pointers: Reference Variables We have also worked with something like pointers when we learned to use reference variables. Suppose we have this function: void getOrder(int &donuts) { cout > donuts; } And we call it with this code: int jellyDonuts; getOrder(jellyDonuts);

Copyright © 2012 Pearson Education, Inc. Something Like Pointers: Reference Variables The donuts parameter, in the getOrder function, points to the jellyDonuts variable. C++ automatically stores the address of jellyDonuts in the donuts parameter.

Copyright © 2012 Pearson Education, Inc. Pointer Variables Pointer variables are yet another way using a memory address to work with a piece of data. Pointers are more "low-level" than arrays and reference variables. This means you are responsible for finding the address you want to store in the pointer and correctly using it.

Copyright © 2012 Pearson Education, Inc. Pointer Variables Definition: int *intptr; Read as: “ intptr can hold the address of an int” Spacing in definition does not matter: int * intptr; // same as above

Copyright © 2012 Pearson Education, Inc. Pointer Variables Assigning an address to a pointer variable: int *intptr; int num = 25; intptr = &num; cout<<intptr<<endl; Memory layout: numintptr 25 0x4a00 address of num : 0x4a00

Copyright © 2012 Pearson Education, Inc.

The Indirection Operator The indirection operator ( * ) dereferences a pointer. It allows you to access the item that the pointer points to. int x = 25; int *intptr = &x; cout << *intptr << endl; This prints 25.

Copyright © 2012 Pearson Education, Inc.

9-13

Copyright © 2012 Pearson Education, Inc. 9.3 The Relationship Between Arrays and Pointers

Copyright © 2012 Pearson Education, Inc. The Relationship Between Arrays and Pointers Array name is starting address of array int vals[] = {4, 7, 11}; cout << vals; // displays // 0x4a00 cout << vals[0]; // displays starting address of vals : 0x4a00

Copyright © 2012 Pearson Education, Inc. 9-16

Copyright © 2012 Pearson Education, Inc. Pointers in Expressions Given: int vals[]={4,7,11}, *valptr; valptr = vals; Cout << valptr <<endl; //memory address cout << *valptr <<endl; //displays 4 cout << *(valptr+1) <<endl; //displays 7 cout << *(valptr+2) <<endl; //displays 11

Copyright © 2012 Pearson Education, Inc. Array Access Array elements can be accessed in many ways: int vals[]={4,7,17}, *valptr; valptr = vals; Array access methodExample array name and []vals[2] = 17; pointer to array and []valptr[2] = 17; array name and subscript arithmetic *(vals + 2) = 17; pointer to array and subscript arithmetic *(valptr + 2) = 17;

Copyright © 2012 Pearson Education, Inc. Dynamic Memory Allocation

Copyright © 2012 Pearson Education, Inc. Dynamic Memory Allocation Can allocate storage for a variable while program is running Computer returns address of newly allocated variable Uses new operator to allocate memory: double *dptr; dptr = new double; Use delete to free dynamic memory: delete dptr;

Copyright © 2012 Pearson Education, Inc.

Program 9-14 (Continued)

Copyright © 2012 Pearson Education, Inc.

Exercises Algorithm Workbench, Page 535: Q24-Q27 Find the Error, Page 536: Q48-Q52