Pointers.

Slides:



Advertisements
Similar presentations
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Advertisements

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.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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.
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. This is.
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.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of 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.
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers and Dynamic Arrays
Review 1 List Data Structure List operations List Implementation Array Linked List.
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: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
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.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers and Dynamic Arrays
Chapter 8 Arrays, Strings and Pointers
Chapter 7 Pointers and C-Strings
Pointers Ritika sharma.
Pointers.
Motivation and Overview
Chapter 10: Pointers Starting Out with C++ Early Objects
Student Book An Introduction
Pointers Revisited What is variable address, name, value?
Pointer.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Pointers Psst… over there.
Pointers and References
Chapter 10: Pointers Starting Out with C++ Early Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Pointers and References
Pointers Psst… over there.
Dynamic Memory Allocation
Pointer Basics Psst… over there.
Chapter 9: Pointers.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Dynamic Variables
Pointers, Dynamic Data, and Reference Types
Pointers Lecture 2 Tue, Jan 24, 2006.
7 Arrays.
C++ Pointers and Strings
Data Structures and Algorithms Introduction to Pointers
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Pointers and dynamic objects
Pointer Basics Psst… over there.
Pointers and References
C++ Pointers and Strings
Introduction to Pointers
Presentation transcript:

Pointers

What Is Pointer i ’y’ c 0021 0022 cp i ’y’ c 0021 0022 cp every variable has memory address char c=’y’; int i=2; address of variable i is 0022 address can used to refer to this variable address can be stored in a variable of special type called pointer (variable) C++ provides an abstraction of pointer pointer is used only to refer to the variable it points to - we usually don’t think of pointers as holding integer (address) just a reference to a variable pointer: a mechanism to uniformly manipulate multiple memory locations in sequence name memory address i ’y’ c 2 0021 0022 cp name memory address i ’y’ c 2 0021 0022 cp

Pointer Declaration pointer variable is declared as follows: typeOfVariablePointedTo *pointerName; example: double *p; int *ip; pointer declarations can be intermixed with ordinary variable declarations: char *cp, c1=’y’, c2=’n’; int i, *ip; star can move to type without changing semantics: int *i, j; is the same as int* i, j; pointer to a pointer is legal and sometimes used: char **cpp;

Reference and Dereference & reference or address of operator: returns the address of a variable, used to assign value to pointer cp = &c1; // until reassigned cp ”points to” c1 * dereference or indirection operator: access the location the pointer points to, two forms for reading: cout << *cp << endl; for writing: *cp = ’G’; note that star at pointer declaration is not a dereference operator – it just signifies that the variable is a pointer.

Pointer Usage pointer can be initialized char *cp2=&c2; int *ip; pointer can point to multiple variables (in sequence) and multiple pointers can point to the same variable what does this code fragment do? int *ip1, *ip2, one=1, two=2; ip1=&one; ip2=ip1; *ip1 = *ip1 + 1; ip1=&two; *ip1 -= 1; cout << *ip2 << ” ” << *ip1;

Constants and Pointers constant pointer: cannot change where pointer points to (can modify value in the location) char c = 'c'; const char d = 'd'; char *const ptr1 = &c; ptr1 = &d; // illegal pointer to a constant: cannot change what pointer points to (can make pointer point elsewhere) const char *ptr2 = &d; *ptr2 = 'e'; // illegal: cannot change d // through dereferencing ptr2 this also declares a pointer to a constant char const *ptr2 = &d; to recognize type, read from right to left

Array Names and Constant Pointers array name is in fact a constant pointer example int *p; // this is a pointer int a[SIZE]; // this is an array // int *const a; plus memory allocation // is equivalent p = a; // now pointer references first // element of an array an array name can be used as name and as pointer: a[3]=22; // as array name: applying indexing p = a; // as pointer a pointer can also be used similarly p[4]=44; // as name since array name is a constant pointer – its modification is not legal a=p; // ERROR!

Pointer Arithmetic array elements are guaranteed to be in continuous memory locations adding one to pointer advances it one memory location of its specified type int a[5], *p = a; p = p + 1; // p points to second element of array gives alternative way to manipulate arrays allowed pointer operations: add/subtract integer, compound assignment, increment, decrement, subtract two pointers of the same type (what’s the purpose of that?) ++p; // moves p one position to the right – points to // third element of array p -=2; // moves p two positions to the left cout << p – a; // prints how many elements between p and a other arithmetic operations, like pointer division or multiplication, are not allowed regular and pointer arithmetic operations can be intermixed *(++p) = 22; // what does this do? caution use only on continuous memory locations terse but obscure indexing may be clearer to understand error prone

Null Pointer/Loose Pointer Problem a pointer that is not initialized holds arbitrary value assigning a value to the location uninitialized pointer points to can lead to unpredictable results: loose (dangling) pointer problem int *ptr; *ptr = 5; // ERROR - loose pointer! what do you think the result of this assignment is? nullptr constant guaranteed to be different from any address in memory convenient for pointer initialization int *ptr = nullptr; assigning nullptr to pointer does not eliminate loose pointer, useful to check if pointer is initialized int *ptr2 = nullptr, i=5; *ptr2 = 5; // ERROR - still loose if (ptr2 == nullptr) // is it initialized? ptr2=&i; cout << *ptr2;

Pointers to Objects pointers can point to objects: myclass{ public: void setd(int i){d=i;}; int getd() const {return d;}; private: int d; }; myclass ob1, *obp=&ob1; members can be accessed using pointers: (*obp).setd(5); parentheses around (*obp) are needed because dot-operator has higher precedence than dereferencing a shorthand -> is used for accessing members of the object the pointer points to: cout << obp->getd();