Pointers Pointers are a unique class of variables whose purpose is to “point” to other variables Pointers allow programmers direct access to memory addresses.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

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.
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.
Kernighan/Ritchie: Kelley/Pohl:
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
Pointers and References (1) THE REFERENCE OPERATOR REFERENCES POINTERS THE DEREFERENCE OPERATOR DERIVED TYPES RETURNING A REFERENCE.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Pointers CSE 5100 Data Structures and Algorithms.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Object-Oriented Programming in C++
Pointers and Dynamic Memory Allocation. Declaring a pointer.
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.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
POINTERS.
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.
Pointers *, &, array similarities, functions, sizeof.
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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Variables and memory addresses
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
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.
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
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.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
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?
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic Arrays
Student Book An Introduction
Pointers and References
Chapter 10: Pointers 1.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Pointers, Dynamic Data, and Reference Types
Objectives You should be able to describe: Addresses and Pointers
Dynamic Memory.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Pointers Pointers are a unique class of variables whose purpose is to “point” to other variables Pointers allow programmers direct access to memory addresses to variables used in the program Great! So?

Motivation for Pointers 1.Essential in dynamic data environments –So far, we have only seen static data (i.e. we know exactly how many variables we need). –Think of arrays and how inconvenient it is to always have to know the size beforehand!

Motivation for Pointers (2) 2.No pointers, no data structures. –Pointers are used to build complex data structures such as: Arrays, Trees, Lists, Stacks, Queues, Rings, etc. 3.C++ cannot pass a physical block of memory as argument into a function. –Rather, pass a variable that “points” to the first element in the structure.

How Does a Pointer “Point?” Pointer variables hold memory addresses. Because of this, a pointer can be used to wander into a machine’s memory space. This ability is often hailed as pointers’ greatest advantage and disadvantage. –Good because of speed and flexibility –Bad because of out-of-bounds memory accesses

How Does a Pointer “Point?” (2) Keep in mind that pointers “point” by holding the memory address of the element to which it is pointing. To obtain the memory address of a variable, we use the address operator &. For example: int x = 10; cout << “The value of x is “ << x << endl; cout << “The address of x is: “ << &x << endl; You would see something similar to: The value of x is 10 The address of x is 0x815acc3f

Declaring Pointers Because the different types of variables take different amounts of memory ( double takes more than float ), we declare a pointer as a specific data type. In general we declare with the following syntax: data-type *pointer-name;

Declaring Pointers (2) An example pointer declaration: int *numAddr;// numAddr points to a // variable of type int A good way to understand declarations is to read them backwards: “numAddr points to an integer”

Simple Example Again, pointers are variables whose value is an address of a location in memory. int num = 6; int *numAddr; // numAddr is an integer pointer numAddr = &num; // numAddr points to num // this is valid because num // is of type int! The variable numAddr holds the address of another variable, num, so it “points” to the variable whose memory address it holds.

Visualizing Pointers int num = 6; int *numAddr; // numAddr is a pointer numAddr = &num; // numAddr points to num

Dereferencing Pointers A pointer variable’s value is a memory address. So how do we obtain the actual content at the address to which it points? We use the deference operator * to obtain this value Take our previous example of the pointer numAddr. In our programs, to obtain the value of num, we do the following: *numAddr

Dereferencing Pointers (2) In other words *numAddr means the value of the variable whose address is stored in numAddr This procedure is called dereferencing: –The value is obtained by first looking up the address the pointer holds and then going to that location in memory to get the value.

Example Program int main() { int *numAddr; //numAddr points to an int int miles, dist; dist = 158; miles = 22; numAddr = &miles; //Assign an address into numAddr //some number (memory address of variable: miles) cout << “The address in numAddr is “ << numAddr << endl; //dererenced here. We obtain 22 cout << “The value pointed to by numAddr is “ << *numAddr << endl;

Example Program (pt 2) numAddr = &dist;//now numAddr points to dist //another hex number (memory address of variable: dist) cout << “The address in numAddr is “ << numAddr << endl; //158 cout << “The value pointed to by numAddr is “ << *numAddr << endl; return 0; } /* Notice that the deference operator * will return the value of the variable that is pointed to, rather than the address of the variable! */

Reference Variables Like pointers, references hold an address Unlike pointers, their values cannot be altered. (We saw that the numAddr pointer pointed to miles and then later, to dist in our example program) References are normally just used for functions (we have seen the pass-by- reference function calls)

Reference Variables (2) To declare a reference variable, we use the form: data-type &new-name = existing-name; For example: double total = 4.5; double &sum = total; In essence what we have done is given total an alias called sum. Warning: We cannot declare a reference without initializing it (i.e. double ∑ is illegal!)

Reference Variables (3) Again references should have the same data type as the variable we wish to give an additional name. This should be obvious since a reference is essentially the variable under a different name.

Simple Example on References int main() { int miles; int &milesRef = miles; milesRef = 42;//same as saying miles = 42; cout << “I traveled “ << miles << “ miles!\n”; // above is same as saying: // cout << “I traveled “ << milesRef << “ miles!\n”; return 0; }

Comparison of References and Pointers Reference: int b, c; Int &a = b; //address of b automatically assigned a = 10; //a is implicitly dereferenced //a cannot be reassigned another address a = c;//illegal! Pointer: int b, c; int *a = &b; //address of b manually assigned *a = 10; //a explicitly dereferenced //a can be reassigned another address a = &c; //a now points to c