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.

Slides:



Advertisements
Similar presentations
An introduction to pointers in c
Advertisements

Lectures 10 & 11.
Programming and Data Structure
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function.
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.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
CSC Programming for Science Lecture 30: Pointers.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
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.
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.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
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.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Pointers Pointers are a unique class of variables whose purpose is to “point” to other variables Pointers allow programmers direct access to memory addresses.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
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.
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.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
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.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
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.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
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.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
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.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
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.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
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.
Student Book An Introduction
Pointer.
Pointers and References
Dynamic Memory Allocation
Pointer Basics Psst… over there.
Pointers, Dynamic Data, and Reference Types
C Programming Pointers
Pointers and dynamic objects
Pointer Basics Psst… over there.
Pointers and References
Presentation transcript:

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 location in memory and stores a value there.  When you refer to the variable by name in your code, the computer must take two steps: 1. Look up the address that the variable name corresponds to 2. Go to that location in memory and retrieve or set the value it contains  C++ allows us to perform either one of these steps independently on a variable with the & and * operators:  &x evaluates to the address of x in memory.  *( &x ) takes the address of x and dereferences it – it retrieves the value at that location in memory. *( &x ) thus evaluates to the same thing as x. 2

1.2 Motivating Pointers  pointers, or Memory addresses, allow us to manipulate data much more flexibly; manipulating the memory addresses of data can be more efficient than manipulating the data itself.  More flexible pass-by-reference.  Manipulate complex data structures efficiently, even if their data is scattered in different memory locations  Use polymorphism – calling functions on data without knowing exactly what kind of data it is. 3

The Nature of Pointers  Pointers are just variables storing integers – but those integers happen to be memory addresses, usually addresses of other variables. A pointer that stores the address of some variable x is said to point to x. We can access the value of x by dereferencing the pointer.  Bellow ptr point to x, that is, the value stored in ptr is 12314, x’s memory address. 4

Declaring Pointers  To declare a pointer variable named ptr that points to an integer variable named x: int *ptr = &x;  int *ptr declares the pointer to an integer value, which we are initializing to the addressof x.  The general scheme for declaring pointers is: data_type *pointer_name; // Add "= initial_value " if applicable pointer name is then a variable of type data type * – a “pointer to a data type value.” 5

Using Pointer Values  Once a pointer is declared, we can dereference it with the * operator to access its value: cout << *ptr; // Prints the value pointed to by ptr // which in the above example would be x’s value  We can use deferenced pointers to change value pointed to: *ptr = 5; // Sets the value of x  Without the * operator, the identifier x refers to the pointer itself, not the value it points to: cout << ptr; // Outputs the memory address of x in base 16 6

const Pointers  There are two places the const keyword can be placed within a pointer variable declaration. This is because there are two different variables whose values you might want to forbid changing: the pointer itself and the value it points to, appear in three cases:  changeable pointer to a constant integer.  declares a constant pointer to changeable integer data.  forbids changing either the address ptr contains or the value it points to. 7

const Pointers (count)  First: declares a changeable pointer to a constant integer. The integer value cannot be changed through this pointer,but the pointer may be changed to point to a different constant integer. const int *ptr;  Second: declares a constant pointer to changeable integer data. The integer value can be changed through this pointer, but the pointer may not be changed to point to a different constant integer. int * const ptr;  Third:forbids changing either the address ptr contains or the value it points to. const int * const ptr; 8

Example const pointer void main () { int x=3; const int *ptr = &x; x = 5; //Error } void main () { int x=3; int y=4; int *const ptr = &x; *ptr = &y; //Error } 9

References  the reference variable x becomes another name – an alias – for the value of y in memory. We can declare a reference variable locally, as well: int y; int &x = y; // Makes x a reference to, or alias of, y  After these declarations, changing x will change y and vice versa, because they are two names for the same thing.  References are just pointers that are dereferenced every time they are used. //show in function  Reference variables must be initialized in their declarations and cannot be reassigned as aliases to other variables 10

11

12

differences between using pointers and using references  References are sort of pre-dereferenced – you do not dereference them explicitly.  You cannot change the location to which a reference points, whereas you can change the location to which a pointer points. Because of this, references must always be initialized when they are declared.  When writing the value that you want to make a reference to, you do not put an & before it to take its address, whereas you do need to do this for pointers. 13

differences between using pointers and using references (count)  The usage of the* and & operators with pointers /references can be confusing.  The* operator is used in two different ways:  When declaring a pointer, * is placed before the variable name to indicate that the variable being declared is a pointer – say, a pointer to an int or char, not an int or char value.  When using a pointer that has been set to point to some value, * is placed before the pointer name to dereference it – to access or set the value it points to.  A similar distinction exists for &, which can be used either  To indicate a reference datatype (as in int &x;), or  To take the address of avariable (as in int *ptr = &x;). 14