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.

Slides:



Advertisements
Similar presentations
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Advertisements

POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
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.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
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.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
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.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
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.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers Data Structures CSI 312 CSI Dept. Dr. Yousef Qawqzeh.
Pointers What is the data type of pointer variables?
Standard Version of Starting Out with C++, 4th Edition
Chapter 9: Pointers.
Lecture 9 Files, Pointers
Chapter 10: Pointers Starting Out with C++ Early Objects
Pointers and Pointer-Based Strings
Pointer.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 10: Pointers Starting Out with C++ Early Objects
Dynamic Memory Allocation
Chapter 9: Pointers.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 9: Pointers.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Constant pointers and pointers to constants
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Pointers and Pointer-Based Strings
COP 3330 Object-oriented Programming in C++
Standard Version of Starting Out with C++, 4th Edition
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Pointers.
Presentation transcript:

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 a variable: int num = -23; cout << &num; // prints address // In hexadecimal 2

Pointer Variables Pointer variable (pointer): variable that holds an address Can perform some tasks more easily with an address than by accessing memory via a symbolic name: –Accessing unnamed memory locations –Array manipulation –etc. 3

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 4

Pointer Variables Assignment: int *intptr; intptr = &num; Memory layout: Can access num using intptr and indirection operator * : cout << *intptr << endl; // prints 25 5 numintptr 25 0x4a00 address of num : 0x4a00

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

Relationship Between Arrays And Pointers Array name can be used as a pointer constant: int vals[] = {4, 7, 11}; cout << *vals; // displays 4 Pointer can be used as an array name: int *valptr = vals; cout << valptr[1]; // displays 7 7

Pointers In Expressions Given: int vals[]={4,7,11}, *valptr; valptr = vals; What is valptr + 1 ? It means (address in valptr ) + (1 * size of an int) cout << *(valptr+1); //displays 7 cout << *(valptr+2); //displays 11 Must use ( ) in expression 8

Array Access Array elements can be accessed in many ways: 9 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;

Initializing Pointers Can initialize at definition time: int num, *numptr = &num; int val[3], *valptr = val; Cannot mix data types: float cost; int *ptr = &cost; // won’t work Can test for an invalid address for ptr with: if (!ptr)... 10

Comparing Pointers Relational operators ( =, etc.) can be used to compare addresses in pointers Comparing addresses in pointers is not the same as comparing contents pointed at by pointers: if (ptr1 == ptr2) // compares // addresses if (*ptr1 == *ptr2) // compares // contents 11

Pointers As Function Parameters A pointer can be parameter Works like reference variable to allow change to argument from within function Requires: 1) asterisk * on parameter in prototype and heading void getNum(int *ptr); // ptr is pointer to an int 2)asterisk * in body to dereference the pointer cin >> *ptr; 3)address as argument to the function getNum(&num); // pass address of num to getNum 12

Pointers As Function Parameters void swap(int *x, int *y) {int temp; temp = *x; *x = *y; *y = temp; } int num1 = 2, num2 = -3; swap(&num1, &num2); 13

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: float *fptr; fptr = new float; new returns address of memory location 14

Dynamic Memory Allocation Can also use new to allocate array: arrayPtr = new float[25]; Can then use [] or pointer arithmetic to access array: for(i = 0; i < 25; i++) *arrayptr[i] = i * i; or for(i = 0; i < 25; i++) *(arrayptr + i) = i * i; Program will terminate if not enough memory available to allocate 15

Releasing Dynamic Memory Use delete to free dynamic memory: delete fptr; Use [] to free dynamic array: delete [] arrayptr; Only use delete with dynamic memory! 16

Returning Pointers From Functions Pointer can be return type of function: int* newNum(); Function must not return a pointer to a local variable in the function Function should only return a pointer: –to data that was passed to the function as an argument –to dynamically allocated memory 17