Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Similar documents
Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Dynamic Allocation of Memory

Dynamic Allocation of Memory

Scope and Parameter Passing

Scope and Parameter Passing

The Critical-Path Algorithm

Building the Abstract Syntax Trees

Minimal Spanning Trees

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

List Iterator Implementation

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Implementing Linked Lists

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Fundamental Data Types

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Friends and Unary Operators

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

The Decreasing-Time Algorithm

Recursive Descent Parsers

Binary Tree Applications

Density Curves Sections

Sampling Distribution Examples Sections 15.4, 15.5

Recursive Linked Lists

Introduction to Compiler Design

Rotations and Translations

The Graphics Pipeline

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Street-Routing Problems

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Programming Languages

Programming Languages

Homework #3 CS2255 Fall 2012

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The Plurality-with-Elimination Method

Scheduling and Digraphs

Magnification and Minification

The Class Construct Part 1

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

The Pairwise-Comparison Method

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Recognition of Tokens

Function Definition Syntax Tree

The Traveling Salesman Problem Brute Force Method

The Graphics Pipeline

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Inheritance: The Fundamental Functions

Solving Recursive Sequences by Iteration

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

The Coefficient of Determination

LR Parsing - Conflicts

Stacks and their Applications

Displaying Distributions - Quantitative Variables

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Abstract Syntax Trees Synthetic and Inherited Attributes

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

The x86 Architecture

The Traveling Salesman Problem Nearest-Neighbor Algorithm

The Class Construct Part 2

The Projection Matrix

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

The Standard Template Library Classes

The Normal Distribution

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Ambient and Diffuse Light

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Pointers, Dynamic Data, and Reference Types

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

MySQL Creating a Database Lecture 3

Lecture 20 Sections Wed, Apr 1, 2009

Introduction to Databases

Dynamic Memory Allocation

The View Frustum. Lecture 9 Sections 2.6, 5.1, 5.2. Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2011

Lab 3. Pointers Programming Lab (Using C) XU Silei

Insertions, Deletions, and Updates

Nondeterministic Programming in C++

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

PHP Queries and HTML Forms Lecture 23

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

XQuery FLOWR Expressions Lecture 35

CS31 Discussion 1E Spring 17 : week 08

The CYK Parsing Algorithm

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Binary Tree Implementation

The Traveling Salesman Problem Cheapest-Link Algorithm

Transcription:

Pointers Lecture 1 Sections 10.1-10.2 Robb T. Koether Hampden-Sydney College Wed, Jan 14, 2015 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 1 / 23

1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 2 / 23

Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 3 / 23

Pointers Definition (Pointer) A pointer is an object that holds the address of another object. All pointers occupy 4 bytes of memory. 4 bytes is sufficient to access 4GB of memory. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 4 / 23

Pointers : 2508 123 Pointer : 2516 2512 2508 2504 2500 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 5 / 23

Declaring Pointers Pointer Declaration int* ptri; float* ptrf; To declare a pointer, write the type of object that the pointer points to, followed by *, followed by the pointer name. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 6 / 23

Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 7 / 23

Null Pointers Pointer Initialized to NULL int* ptri = NULL; A null pointer is a pointer that has the value zero. A null pointer does not point to any object. The constant NULL has the value 0. It is a good practice to initialize a pointer to NULL if it will not immediately point to an object. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 8 / 23

Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 9 / 23

The Address Operator Pointer Initialized to an Object int i; int* ptri = &i; // &i is address of i The unary prefix address operator & returns the address of an object. The address may be assigned to a pointer. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 10 / 23

Example Example (Example) Address Operator.cpp Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 11 / 23

Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 12 / 23

Dereferencing Pointers Dereferencing Pointers int i = 10; int* ptri = &i; cout << *ptri << endl; // ptri points to i // Print i The unary prefix dereference operator * is used to dereference a pointer. When a pointer is dereferenced, it returns the object that it points to. Never, ever dereference a null pointer. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 13 / 23

Example Example (Example) Dereference Pointers.cpp Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 14 / 23

Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 15 / 23

Pointers to void A pointer to void, i.e., a void* object, is a pointer that does not point to any particular kind of object. There is no such thing as a void object. void is treated as a neutral type for situations where the type does not matter or is unknown. A void pointer may hold the address of any type of object. A void pointer may not be dereferenced. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 16 / 23

Assignment of Pointers The pointer assignment ptr1 = ptr2 is legal if and only if ptr1 and ptr2 point to the same type of object, or ptr2 is NULL, or ptr1 is a pointer to void. In all other cases, we would have to cast p2 to be compatible with ptr1. Assigning pointers to different types can cause serious problems. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 17 / 23

Pointer Compatibility Pointer Compatibility int i = 123; float f = 4.56; int* ptri = &i; float* ptrf = &f; // Swap ptri and ptrf void* temp = ptri; ptri = (int*)ptrf; ptrf = (float*)ptri; What are the values of *ptri and *ptrf? Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 18 / 23

Pointer Compatibility &i ptri &f ptrf 0x0000007b 0x4091eb85 i = 123 f = 4.56 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 19 / 23

Pointer Compatibility &i ptri &f ptrf 0x0000007b 0x4091eb85 i = 123 f = 4.56 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 20 / 23

Example Example (Example) Ptr Compatibility.cpp Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 21 / 23

Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 22 / 23

Assignment Homework Read Sections 10.1-10.2. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 23 / 23