Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
(continue) © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Dale Roberts, Lecturer Computer Science, IUPUI.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
Pointers and Pointer-Based Strings
Pointers Kingdom of Saudi Arabia
Chapter 5 - Pointers and Strings
5.1 Introduction Pointers Powerful, but difficult to master
7 C Pointers.
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 18 Pointers – Part II
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
Presentation transcript:

Pointers Class #9 – Pointers

Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references to do Pass-By-Reference. We can use Pointers to create Dynamic Data structures like Linked Lists, Stacks, Queues and Trees.

Pointers Pointers are a type of variable, just like int, double, etc., except instead of storing a value, they store a memory address of another variable. In this sense, a variable directly references a value, and a pointer indirectly references a value.

Pointers Pointers, just like other variables, must be declared before they can be used. For example, the declaration int *countPtr; declares a variable countPtr to be of type int * (a pointer to an int value) This is read as “ countPtr is a pointer to an int ”. Each variable being declared as a pointer must be preceded by an asterisk ( * ). Also, although not required, declaring any pointer value with the name ending in Ptr is a good idea.

Initializing Pointers A Pointer may be initialized to 0, NULL, or an address. NULL is a Symbolic constant defined in to represent the value 0. A Pointer that is assigned 0 or NULL points to nothing.

Initializing Pointers The address operator, (&), is a unary operator that returns the memory address of it ’ s operand. This is how we can assign a memory address to a pointer variable. int y=5; //Declare an int, assign the value of 5 int *yPtr; //Declare a pointer to an int yPtr = &y; //Assign yPtr the memory address of y

Pointer Operators Example int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y yPtr “ points to ” y yPtr y 5 yptr y 5 address of y is value of yptr

The Dereferencing Operator The * operator, referred to as the indirection, or dereferencing operator, returns an alias to what the pointer is pointing to. In the previous example, the line cout <<*yPtr; //Will Print 5 Will print the value of the variable that *yPtr points (which is y, which is 5) Basically, *yPtr “ returns ” y Operations like below are also legal *yPtr = 7; //Changes y to 7

Address of and Dereference The address of (&) and dereference (*) operators are actually inverses of each other. They cancel each other out *&myVar == myVar and &*yPtr == yPtr

CISC370 - Fischer 1// Fig. 5.4: fig05_04.cpp 2// Using the & and * operators 3#include 4 5using std::cout; 6using std::endl; 7 8int main() 9{9{ 10 int a; // a is an integer 11 int *aPtr; // aPtr is a pointer to an integer a = 7; 14 aPtr = &a; // aPtr set to address of a cout << "The address of a is " << &a 17 << "\nThe value of aPtr is " << aPtr; cout << "\n\nThe value of a is " << a 20 << "\nThe value of *aPtr is " << *aPtr; cout << "\n\nShowing that * and & are inverses of " 23 << "each other.\n&*aPtr = " << &*aPtr 24 << "\n*&aPtr = " << *&aPtr << endl; 25 return 0; 26} The address of a is the value of aPtr. The * operator returns an alias to what its operand points to. aPtr points to a, so *aPtr returns a. Notice how * and & are inverses The address of a is 006AFDF4 The value of aPtr is 006AFDF4 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 006AFDF4 *&aPtr = 006AFDF4

Function Passing Let ’ s go back old (read: previous test) topic. Passing arguments to functions. They are two well known ways – passing by value, and passing by reference. We can do pass by reference two different ways. Passing by Reference using references, or passing by reference using pointers. You actually did this in Lab 3.

Function Passing So let ’ s think about this for a second. We ’ re going to pass variables, by reference, using pointers. Pointers hold … ? Memory Addresses and what operator did we just see that will give us the memory address of a variable? The ampersand ( & ) operator This is how we call a function that uses call by reference using pointers – we have to send it the memory address.. so the function call looks like myfunc( &my_var ); //calls function myfunc with mem //address of my_var

Function Passing So we call a function that uses Pointer arguments with the syntax of myfunction( &my_var ); So now we send a memory address to our function. To actually do anything with it, we need to deference it, both in the function definition and in the function body. myfunction( int *my_varPtr ) { *my_varPtr = *my_varPtr * *my_varPtr; }

Const Pointers Const Pointers, just like any const variable, is unable to be changed once it is initialized. Const Pointers are Pointers that always point to the same memory location. These must be initialized when they are declared. Remember, when your declaring a pointer variable, you have to declare it a type (int, double, etc.) – C++ also makes the distinction between a regular int and a const int.

Const Pointers So basically this leads to some screwy syntax with const pointers.. here it is. Assume x is declared as int x = 5; Non Constant Pointer to Non Constant Data int *myPtr = x; Non Constant Pointer to Constant Data const int *myPtr = &x; Constant Pointer to Non Constant Data int *const myPtr = &x; Constant Pointer to Constant Data const int *const Ptr = &x;

CISC370 - Fischer 1// Fig. 5.13: fig05_13.cpp 2// Attempting to modify a constant pointer to 3// non-constant data 4#include 5 6int main() 7{7{ 8 int x, y; 9 10 int * const ptr = &x; // ptr is a constant pointer to an 11 // integer. An integer can be modified 12 // through ptr, but ptr always points 13 // to the same memory location. 14 *ptr = 7; 15 ptr = &y; return 0; 18} Error E2024 Fig05_13.cpp 15: Cannot modify a const object in function main() Changing *ptr is allowed - x is not a constant. Changing ptr is an error - ptr is a constant pointer.

Pointer Arithmetic Welcome to the world of weird programming errors. This is another example of the powerful but dangerous nature of pointers. Pointer Arithmetic is so error prone, it ’ s not allowed in Java or C#. This doesn ’ t mean don ’ t use – you may have, or at least understand it so you can understand other people ’ s code. So master this.

Pointer Arithmetic So again, what is an array really? That ’ s right, a const pointer. So, we can create a Pointer to the first element of an array with code like int b[5] = { 0 }; int *bPtr; bPtr = b; * note, above line equivalent to bPtr = &b[0];

Pointer Arithmetic Normally, when we would want to access the 4 th element in our array, we ’ d use notation like b[3]; but, we can also do ( bPtr + 3); //actually does address of bPrt + 3 * 4 this is called using Pointer/Offset notation. We can also access pointers using subscripts bPtr[3]; // same as above this is called, you guessed it, Pointer/Subscript notation.

Thoughts on Previous Although you can use Pointer/Subscript notation on Pointers, and you can use Pointer/Offset notations with arrays, try not to unless you have a good reason. No technical reason, it is just confusing for people reading your code.

Null Pointers When you begin a program, you may have some pointers declared, but are not yet set to any value, in this case you should set them to NULL. NULL is a special value that indicates a pointer is unused. For each pointer type, there is a special value -- the "null pointer" -- which is distinguishable from all other pointer values and which is not the address of any object. NULL is defined as 0 (zero) in C++.

Arrays of Pointers Normally, we ’ re used to Arrays containing ints, doubles, etc We can also make arrays of Pointers. This is most commonly seen with Arrays of C- Style strings.

Array of Pointers const char* suit = { “ Clubs ”, ” Diamonds ”, ” Hearts ”, ” Spades ” ) This basically says “ each element is of type pointer to char ”

Why Null Pointers? When we declare (but not initialize) ANY variable, what value does it contain? What can ’ t we do to a variable if we have no idea what it contains? Null Pointers give us a way to compare and see if something is initialized.

Comparing Pointers To test if a Pointer is null, you can either by either int *intPtr=NULL; if (intPtr==NULL) or if (intPtr== ‘ 0 ’ ) These both are equivalent. I like the first convention (more readable), but either is acceptable.

Arrays of Pointers Arrays can contain pointers Each element of not in the array, only pointers to the strings are in the array suit is a pointer to a char * (a string) The strings are suit array has a fixed size, but strings can be of any size suit[3] suit[2] suit[1] suit[0]’H’’e’’a’’r’’t’’s’ ’\0’ ’D’’i’’a’’m’’o’’n’’d’’s’ ’\0’ ’C’’l’’u’’b’’s’ ’\0’ ’S’’p’’a’’d’’e’’s’ ’\0’