Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

C Language.
Data Structures Using C++ 2E
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Microsoft Visual C++.NET Chapter 61 Memory Management.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
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.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
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.
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.
Windows Programming Lecture 03. Pointers and Arrays.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 4 Linked Lists.
14th September IIT Kanpur
Chapter 10: Pointers 1.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9: Pointers and String
Presentation transcript:

Chapter 11 – Pointer Variables

Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type –Usually no space between so more obvious –Space allowed, so 1 or more would still work u Reserves space for storage u Must initialize or assign address Lesson 11.1

Assigning an Address u Use the "address of" operator (&) u General form: pointer_variable = &ordinary_variable Lesson 11.1 Name of the pointerName of ordinary variable

Using a Pointer Variable u Can be used to access a value u Unary operator * used * pointer_variable –In executable statement, indicates value u Common practice to use suffix ptr for pointer variables –Example: width_ptr Lesson 11.1

Pointer Operators u Ampersand & –Address of u Asterisk * –Get value at the address Lesson 11.1

Uses of * u Binary multiplication operator volume = height * depth * width; u Declaration specifier indicating address to be stored in variable's memory cell double* height_address; u Unary operator indicating to get value or access memory cells at addressed *height_address = 10.5; Lesson 11.1

Uses of & u Declaration of function header to indicate a reference void function1 (double&) u In executable statement to indicate "address of" height_address = &height; Lesson 11.1

Spacing a * u Somewhat flexible in declaration or header double* height; double *height; double * height; Lesson 11.1 Confusing – looks like multiplication usage u For multiple pointer declarations double *height *width; double* height; double* width; or

Transferring Addresses to Functions u In declaration and header use type* in argument list type function (type*, type*); type funcName (type* name, type* name) u In function call use &variable in argument list to pass address identifier = funcName (&name1, &name2); Lesson 11.2

Using Pointer for Array Transfer Lesson 11.3 rtype function (type*, int); function (array, num); rtype function (type* b, int num_elem) { code using b with brackets (b[n]) } Declaration Call Header Function Body

Example: Lesson 11.3 void function2 (double*, int); int main ( ) { double c[5] = {2.1, 3.5, 6.4, 1.9, 4.5}; function2 ( c, 5); } void function2 (double* b, int num_elem) Name of array is address

Pointer Variables and Math u Declare variable as pointer double* b; –Holds address u Can perform addition and subtraction –Purpose of add is to advance to subsequent cell b + 1 (double is 8 bytes, so adds 8 bytes) –Subtraction goes back to previous memory cell b – 1 (goes back 8 bytes) Lesson 11.4

Returning Array Address from Function Lesson 11.5 Example: Function declaration and header double* get_array_address (double [ ] ); double* get_array_address (double c[ ] ) Now a return statement with variable that indicates address in the function body return c; double* Could also use

Pointer Variables u Point to 2, 4, or 8 bytes of memory depending on type of variable u Can point to entire array –Holds address of beginning of array –Pointer declaration indicates size of memory greater than single value Lesson 11.6

Creating Pointer to Arrays u Example: double (*f) [2]; Lesson 11.6 Declares f to be a pointer One dimensional array of size 2 Array elements of type double u Example: double (*h) [3] [5]; Declares h to be a pointer Two dimensional array of size 3 * 5 (15 elements) Array elements of type double ( ) are REQUIRED

typedef Statement u Used to create an alias for a data type –Note – not a new data type u Basic form typedef type synonym_1, synonym_n; Lesson 11.6 Any valid data type List of valid identifiers (any number)

Uses of typedef u Improve readability and understandability of the code u Easier to modify programs that are implementation dependent Lesson 11.6

Arrays and typedef u General form: typedef type name [size]; –type is the type of values in the array –name is the alias to be used as the data type in a declaration –size is array size Lesson 11.6

typedef for Pointer to Array u General form: typedef type (*name) [size]; –type is type of values in array –name is the alias to be used as data type in declaration –size is array size Lesson 11.6

Returning a Pointer u Given the example: typedef double (*array_ptr) [2]; –Creates alias array_ptr for declarations of pointers to 1-D arrays of double with size 2 u array_ptr function2 (argument); –Indicates pointer to 1-D array is returned from function Lesson 11.6

Multidimensional Arrays u C++ sees array of arrays u Example: int a[2] [3] [4]; –Can work with whole (or 1) array –Can use 2 arrays of [3] [4] –Can use 6 arrays of [4] –Can use 24 integers –Pointers: int (*b)[3] [4], (*c)[4], *d, a t Can assign addresses with & operator ( b = &a[0]; ) Lesson 11.6

Pointers to Objects u Special arrow operator -> –Negative and greater than symbol with no space between –Used to access members with object's address u Declaring pointer Class_name* ptr_name; u Initializing pointer ptr_name = &object_name; u Calling member function (2 argument example) ptr_name -> function_name (arg1, arg2) Lesson 11.7

Pointers as Data Members u General form class Class_name { private: type* pointer_name; public: type function_name ( ); }; Lesson 11.8 Name of the class Any valid data type including name of struct or class Name of member function

Dynamic Memory Allocation u Optimize memory space with new and delete operators –Reserve and unreserve memory while program is execution –Pointer variables important because operators work with addresses of memory reserved Lesson 11.9

new Operator u General form: new type [num_elements] –type is data type of array elements –num_elements is number of array elements u Reserves memory but does NOT fill with values –new returns address of memory it reserves –Value assigned to pointer variable of same type Lesson 11.9 type* array_address; array_address = new type [num];

delete Operator u General form: delete [ ] array_address; –array_address is pointer variable u Releases memory stored at address indicated u Knows size of memory reserved by new and releases memory for all the array u delete address; –deletes memory for single element Lesson 11.9

Summary u Declare and initialize pointer variables u Pass addresses to functions u Return an address from a function u Reserve memory during execution u Link classes with accessor functions Learned how to: