Pointers Psst… over there.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
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.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
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.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
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.
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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Dynamic memory allocation and Pointers Lecture 4.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Review 1 List Data Structure List operations List Implementation Array Linked List.
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.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
A Computer Science Tapestry 12.1 Pointers, Memory, Abstractions l A pointer is the a variable/value that is a memory address  Addresses like 1, 2, 3,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
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.
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 What is the data type of pointer variables?
CS31 Discussion Jie(Jay) Wang Week8 Nov.18.
Chapter 7 Pointers and C-Strings
Introduction to Programming Using C
Motivation and Overview
Pointers.
Pointers and Pointer-Based Strings
FIGURE 9-5 Integer Constants and Variables
COMP 2710 Software Construction Pointers
Dynamic Memory CSCE 121 J. Michael Moore.
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.
void Pointers Lesson xx
Andy Wang Object Oriented Programming in C++ COP 3330
Pointer Data Type and Pointer Variables
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Pointer Basics Psst… over there.
Chapter 9: Pointers.
Variables with Memory Diagram
Chapter 9: Pointers.
Pointers & Functions.
Chapter 12 Pointers and Memory Management
Reference Parameters.
5.1 Introduction Pointers Powerful, but difficult to master
Pointers Lecture 1 Thu, Jan 15, 2004.
Overloading functions
CHAPTER 2 Arrays and Vectors.
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
Pointers and dynamic objects
Pointers & Functions.
Pointer Data Type and Pointer Variables
Pointer Basics Psst… over there.
Pointer Data Type and Pointer Variables
Pointers, Memory, Abstractions
Dynamic Memory CSCE 121.
Presentation transcript:

Pointers 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

Pointers are the Force Pointers the glue that link portions of code Indirection / Sharing resources / Dynamic memory

Pointers are the Force Pointers have a dark side Can mess things up real good

Pointer Types Pointers can only point to a certain type of data Need to know how to interpret what we find if x is an int pointer we have issues… 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:

* 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

* Location BUT, This says Ways to make two pointers: x stores a pointer to an int y stores an int Ways to make two pointers:

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

Getting Addresses &x  0x01 & : 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 memory locations Our name Memory location Value 0x00 x 0x01 100 p 0x02 q 0x03 0x04

Accessing Pointees * outside of declaration is dereference operator *p "the thing at the address stored in p" *p = 100 Our name Memory location Value 0x00 x 0x01 100 p 0x02 q 0x03 0x04

Dereferenced Use dereference to get / set values Value 100 0x00 x 0x01 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 }

Pointers and Functions Functions can take pointers: C equivalent to pass by ref #include <iostream> using namespace std; //C++ style void doubleVariable(int& x) { x = x * 2; } //C style void doubleVariableCStyle(int* x) { *x = (*x) * 2; } int main() { int num = 10; cout << num << endl; doubleVariable(num); cout << num << endl; doubleVariableCStyle(&num); cout << num <<#include <iostream> using namespace std; //C++ style void doubleVariable(int& x) { x = x * 2; } //C style void doubleVariableCStyle(int* x) { *x = (*x) * 2; int main() { int num = 10; cout << num << endl; doubleVariable(num); doubleVariableCStyle(&num); return 0; endl; return 0; }

Return Pointers Can return a pointer from a function: