Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.

Slides:



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

Programming and Data Structure
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
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.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
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.
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.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
Chapter 13: 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.
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++
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 Chapter 9: Pointers.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
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.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Variables and memory addresses
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 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.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Chapter 11 Mechanisms for developing flexible list representations Pointers and dynamic memory.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
Pointers and Dynamic Arrays
Standard Version of Starting Out with C++, 4th Edition
Chapter 9: Pointers.
Introduction to Programming
Pointer.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers and dynamic objects
Alternate Version of STARTING OUT WITH C++ 4th Edition
Pointers, Dynamic Data, and Reference Types
Pointers.
C++ Programming Lecture 17 Pointers – Part I
C Programming Lecture-8 Pointers and Memory Management
Pointers and dynamic objects
Standard Version of Starting Out with C++, 4th Edition
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Programming Pointers

COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory addresses

COMP104 Lecture 32 / Slide 3 Pointers l Usefulness n Necessary for dynamic objects –Objects whose memory is allocated during program execution. –Dynamic objects can survive after the function ends in which they were allocated. –Dynamic objects allow flexible-sized arrays

COMP104 Lecture 32 / Slide 4 Basics l Pointer n Object whose value represents the location (memory address) of another object n C++ has pointer types for every type of object –Pointers to int objects –Pointers to char objects –Pointers to user-defined objects (e.g., Temperature ) n Even pointers to pointers –Pointers to pointers to int objects

COMP104 Lecture 32 / Slide 5 Syntax l Examples of uninitialized pointers int *iPtr; // iPtr is a pointer to an int char *s; // s is a pointer to a char Rational *rPtr; // rPtr is a pointer to a // Rational l Examples of initialized pointers int i = 1; char c = 'y'; int *ptr; // ptr is an int pointer ptr = &i; // ptr is assigned the address of i char *t; // t is a char pointer t = &c; // t is assigned the address of c Indicates pointer object “&i” means “the address of i”

COMP104 Lecture 32 / Slide 6 t points to address of a character i 'y'c 1 t ptr Memory Depiction int i = 1; char c = 'y'; int *ptr = &i; char *t; t = &c

COMP104 Lecture 32 / Slide 7 Address Operator l Another example: int i = 1; int j = 2; int *ptr; ptr = &i; // ptr points to location of i *ptr = 3; // contents of i are updated ptr = &j; // ptr points to location of j *ptr = 4; // contents of j are updated cout << i << " " << j << endl;

COMP104 Lecture 32 / Slide 8 Indirection Operator An asterisk (“*”) has two uses for pointers n We have already seen that in a declaration an asterisk indicates that the object is a pointer. char *s; // s is of type pointer to char n In expressions, an asterisk before a pointer indicates that we want to dereference the pointer (“content of” operator ). int i = 1, j; int *ptr; // ptr is an int pointer ptr = &i; // ptr points to i j = *ptr + 1;// j is assigned 2 cout << *ptr << j << endl; // display " 12 "

COMP104 Lecture 32 / Slide 9 Null Address 0 (NULL) is a pointer constant that represents the empty or null address n Indicates that pointer is not pointing to storage of a valid object n Cannot dereference a pointer (refer to the object pointed at by a pointer) whose value is null int *ptr; ptr = 0; cout << *ptr << endl; // invalid, ptr // does not point to // a valid int

COMP104 Lecture 32 / Slide 10 Member Indirection l Consider Rational r(4,3); Rational *rPtr = &r; To select a member of r through indirection using rPtr, operator precedence requires we do the following (*rPtr).Display(); This looks strange, so C++ provides the indirect member selector operator -> rPtr->Display(); Calls member Display of the object to which rPtr points (r)

COMP104 Lecture 32 / Slide 11 Traditional Pointer Usage in C void IndirectSwap(char *Ptr1, char *Ptr2) { char c = *Ptr1; *Ptr1 = *Ptr2; *Ptr2 = c; } int main() { char a = 'y'; char b = 'n'; IndirectSwap(&a, &b); cout << a << b << endl; return 0; }

COMP104 Lecture 32 / Slide 12 Pass by Reference in C++ void IndirectSwap(char& y, char& z) { char temp = y; y = z; z = temp; } int main() { char a = 'y'; char b = 'n'; IndirectSwap(a, b); cout << a << b << endl; return 0; }

COMP104 Lecture 32 / Slide 13 Example int arr[5], *ptr, *ptr2, j; for (int i=0;i<5;i++) arr[i] = i; ptr = arr; cout << *(ptr+3) << endl; // 3 cout << *ptr << endl; // 0 ptr++; cout << *ptr << endl; // 1 ptr += 2; cout << *ptr << endl; // 3 j = *ptr * 10; cout << j << endl; // 30 ptr2 = ptr - 2; cout << *ptr2 << endl; // 1 cout << ptr + 1 << endl; // address arr arr+1 arr+2 arr+3 arr+4 ptr