Pointer.

Slides:



Advertisements
Similar presentations
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
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.
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.
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.
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 © 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 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
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.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers *, &, array similarities, functions, sizeof.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
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.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
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 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.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Lecture 5 Pointers 1. Variable, memory location, address, value
Pointers What is the data type of pointer variables?
Standard Version of Starting Out with C++, 4th Edition
Pointers Introduction
Chapter 9: Pointers.
Lecture 9 Files, Pointers
Chapter 10: Pointers Starting Out with C++ Early Objects
EPSII 59:006 Spring 2004.
Pointers and Pointer-Based Strings
Pointers Revisited What is variable address, name, value?
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Lecture 6 C++ Programming
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
Pointers and Dynamic Variables
5.1 Introduction Pointers Powerful, but difficult to master
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
Standard Version of Starting Out with C++, 4th Edition
Pointers and pointer applications
Pointers.
Presentation transcript:

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. x = 1.0; intarray[2] = 17; Constants and expressions are not lvalues

Memory and Data Representation Every lvalue is stored somewhere in memory and therefore has an address Once allocated, the address of an lvalue never changes, even though its contents may change Depending on type of data, different lvalues require different amounts of memory The address of an lvalue is itself data that can be manipulated and stored in memory

Pointer Declaration A pointer is a variable storing the address of another variable Declaration: type *ptr; Example: int *p1, *p2; p1 and p2 are referred to as pointers-to-int, and may contain the addresses of integers

Pointer Operations C++ defines two operators that manipulate pointer values: & address-of * value-pointed-to (dereference) Operand of & must be lvalue, and & returns the address of the lvalue Operand of * must be a pointer, and * returns the value pointed to by the pointer

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 = -99; cout << &num; // prints address // in hexadecimal

Pointer Variables Definition: Read as: int *intptr; Read as: “intptr can hold the address of an int” Spacing in definition does not matter: int * intptr; // same as above int* intptr; // same as above

Pointer Variables Assigning an address to a pointer variable: int *intptr; intptr = &num; Memory layout: num intptr 25 0x4a00 address of num: 0x4a00

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.

Special pointer NULL NULL is the value of a pointer that does not currently point to any data (usually value 0) Can not dereference a NULL pointer

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

Example void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int num1 = 2, num2 = -3; swap(&num1, &num2);

(Program Continues)

Another Example { *ip = 0; } To modify value of argument, use pointer void SetToZero(int *ip) { *ip = 0; } which makes “ip” a pointer to the location of the value of the argument of the call SetToZero(&x);

Advantage of use of & operator When calling a function using func(x); you know the value of the variable x will not change, whereas calling a function using func(&x); is allowed to change the value of x Easier to predict effects of function call

SwapInteger function When sorting arrays, we wanted to swap values at low and high, but the following will not work SwapIntegers(array[low], array[high]); Using pointers allows the following: void SwapIntegers(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } and SwapIntegers(&array[low], &array[high]);

Returning Multiple Results Convert time (minutes) to hours and minutes void ConvertTime(int time, int *hours, int *mins) { *hours = time / 60; *mins = time % 60; } Call with ConvertTime(t, &h, &m);

Or Use Two functions int Hours(int time) { return (time / 60); } int Minutes(int time) return (time % 60);