Pointer Basics Psst… over there.

Slides:



Advertisements
Similar presentations
 Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
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.
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
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. Overview  What are Pointers?  How to use Pointers?  Use of Pointers.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
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.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
Review 1 List Data Structure List operations List Implementation Array Linked List.
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.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
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 and Dynamic Arrays
CS31 Discussion Jie(Jay) Wang Week8 Nov.18.
Chapter 7 Pointers and C-Strings
Motivation and Overview
Pointers.
Learning Objectives Pointers Pointer in function call
Pointers and Pointer-Based Strings
Student Book An Introduction
Introduction to Programming
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointers Psst… over there.
Chapter 9: Pointers.
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Pointers & Functions.
Chapter 12 Pointers and Memory Management
Pointers Lecture 1 Thu, Jan 15, 2004.
C++ Pointers and Strings
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
POINTER CONCEPT 4/15/2019.
Pointers & Functions.
Pointer Basics Psst… over there.
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
CS148 Introduction to Programming II
POINTER CONCEPT 8/3/2019.
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Presentation transcript:

Pointer Basics Psst… over there

Pointers Pointer : data type with value that is a memory address x is a pointer Stores location to find value Our name Memory location Value 0x00 0x01 12.5 x 0x02 0x03 0x04

Declaring a Pointer * in a declaration means "pointer to" x can point to an integer value: y can point to a double value:

Value Uninitialized pointers point to random location What value does p point to? Our name Memory location Value 0x00 0x01 100 p 0x02 0x38232 0x03 0x04

Getting Addresses & is address of operator &x "Give me the memory address of x" &x  0x01 Our name Memory location Value 0x00 x 0x01 100 p 0x02 0x38232 0x03 0x04

Initializing Pointers Use address of a variable to set pointers Our name Memory location Value 0x00 x 0x01 100 p 0x02 0x03 0x04

Using Pointers Can work directly with pointers Value = copies <, > == compare the memory locations Our name Memory location Value 0x00 x 0x01 100 p 0x02 q 0x03 0x04

Accessing Pointees * is the dereference operator *p says Value Outside of variable declarations *p says "Give me the value at the address in p" Our name Memory location Value 0x00 x 0x01 100 p 0x02 q 0x03 0x04

Dereferenced Can use dereferenced address to get or set value Value Our name Memory location Value 0x00 x 0x01 100 p 0x02 0x03 0x04

Dereferenced Can use dereferenced address to get or set value Value Our name Memory location Value 0x00 x 0x01 200 p 0x02 0x03 0x04

Dereferenced Can use dereferenced address to get or set value Value Our name Memory location Value 0x00 x 0x01 200 p 0x02 0x03 0x04

Pointer to Pointer Can point to a pointer Value Address of the address of a value Two dereferences to access Our name Memory location Value 0x00 i 0x01 200 p 0x02 0x1 q 0x03 0x2 0x04 int i = 5; int* p = &i; int** q = &p; cout << q << endl ; //2 cout << *q << endl ; //1 cout << **q << endl ; //200

NULL Pointers NULL means nothing C C++ NULL pointer points to nothing NULL is keyword that has value 0 C++ NULL usually works, but supposed to use nullptr or 0

Using NULL NULL or 0 used to safely indicate pointer does not have a pointee p = NULL; //C style p = 0; //older C++ p = nullptr; //modern C++ if(p) { //only get here if not null/0 }

Syntax Issues

* Location These two both say x is a pointer to an int: Type: Pointer to Int Name: X

* Location BUT, This says x stores a pointer to an int y stores an int

Typedef Typedef can eliminate some syntax issues int* x, y; //x is a pointer to an int, y is an int int *x, *y; //x and y both pointers to ints typedef int* intPtr; //intPtr means int* intPtr x, y; //x and y both pointers to ints

Const & Pointers Pointer's value (address) can be constant Can't change where it points Values they point to can be constant Can't change the value of whatever it points to

Read Backwards p1 is a constant pointer to an int Can't point at a new location Value we are pointing to can be changed

Read Backwards p1 is a pointer to an int that is constant Can point at a new location Value we are pointing to cannot be changed

Read Backwards p1 is a constant pointer to an int that is constant Cannot point at a new location Value we are pointing to cannot be changed

Read Backwards Const vars must be pointed to with const value pointers: