Pointers. Variable Declaration. Chapter 10

Similar documents
Homework #3 CS2255 Fall 2012

Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Arrays, Pointers and Memory Management

C++ for Java Programmers

CS2255 HOMEWORK #1 Fall 2012

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Week 3: Pointers (Part 2)

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Assist. Prof. Dr. Caner ÖZCAN

CSC 211 Intermediate Programming. Arrays & Pointers

BITG 1233: Introduction to C++

What is an algorithm?

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Pointers, Dynamic Data, and Reference Types

Lecture 23: Pointer Arithmetic

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

C++ Programming: From Problem Analysis to Program Design, Third Edition

Exam 3 Chapters 7 & 9

Pointers in C/C++ 1 Memory Addresses 2

CS201- Introduction to Programming Current Quizzes

The C++ Language. Arizona State University 1

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

CHAPTER 3 Expressions, Functions, Output

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Chapter 9. Pointers and Dynamic Arrays

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

BITG 1113: Array (Part 1) LECTURE 8

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Pointers, Arrays and C-Strings

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Structured Data. CIS 15 : Spring 2007

CSC 211 Intermediate Programming. Pointers

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

CS2141 Software Development using C/C++ C++ Basics

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

pointers + memory double x; string a; int x; main overhead int y; main overhead

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

7 Arrays. 7.1 Arrays Hold Multiple Values TOPICS. CONCEPT: An array allows you to store and work with multiple values of the same data type.

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Review of Important Topics in CS1600. Functions Arrays C-strings

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

I/O Streams and Standard I/O Devices (cont d.)

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Pointer Data Type and Pointer Variables

Chapter 3 Problem Solving and the Computer

A First Program - Greeting.cpp

CS 103 Lab - Party Like A Char Star

1. In C++, reserved words are the same as predefined identifiers. a. True

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Chapter 11: Pointers

Pointers. Memory. void foo() { }//return

CMPS 221 Sample Final

C++ Programming Chapter 7 Pointers

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Reference operator (&)

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Integer Data Types. Data Type. Data Types. int, short int, long int

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Objectives. In this chapter, you will:

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

What have we learned about when we learned about function parameters? 1-1

W3101: Programming Languages C++ Ramana Isukapalli

Pointers and Arrays. Introduction To Pointers. Using The "Address Of" The Operator & Operator. Using The Dereference. The Operator * Operator

CHAPTER 3 BASIC INSTRUCTION OF C++

First of all, it is a variable, just like other variables you studied

Pointers and Dynamic Arrays

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

C++ For Science and Engineering Lecture 15

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

C++ Programming for Non-C Programmers. Supplement

Transcription:

Pointers Chapter 10 Variable Declaration When a variable is defined, three fundamental attributes are associated with it: Name Type Address The variable definition associates the name, the type, and the address of the location where the variable is to be stored Chapter10 2 1

Names and Addresses The number of storage locations allocated for the variable is determined by the type, e.g., one byte for a char Chapter10 3 Accessing Values and Addresses The value of a variable is accessed by means of the variable name A variable s address is accessed by means of the address operator: & int x = 42; cout << " The value of x is " << x; cout << " x is stored in location " << &x; cout << " The size of X is " << sizeof(x) << " bytes\n"; Chapter10 4 2

Pointer Variables The & operator returns the address of the variable to which the operator is applied What might we want to do with that address, other than print it? How about store it? The type of variable that stores an address is a pointer variable, usually just called a pointer Chapter10 5 Pointer Variables Just as int variables are designed to store and work with integers, pointers are designed to store and work with addresses A pointer can be thought of as a locator ; it tells us where to locate another variable. That variable is of a specific type, so we declare a pointer as a pointer to a type: int * ptr1; //ptr1 is a pointer to an int // ptr1 can hold the address of an int float * ptr2;// ptr2 is a pointer to a float // ptr2 can hold the address of a float The * (which is part of the data type) in front of the variable name indicates the variable is a pointer Chapter10 6 3

Pointer Variables Example: int X = 25; int * ptr = &X; // place the address of X in pointer ptr Chapter10 7 Pointers are useful for the following: Indirectly manipulating data stored in other variables Working with memory locations that regular variables don t give you access to Working with strings and arrays Creating new variables in memory while the program is running Creating arbitrarily-sized lists of values in memory Chapter10 8 4

Pointer Variables Program #include <iostream> using namespace std; int main ( void ) { int X = 25; int *Ptr; The value in X is 25 The address of X is 0x7e00 Ptr = &X; // Store the address of X in Ptr cout << "The value in X is " << X << endl; cout << "The address of X is " << Ptr << endl; return 0; } // main Chapter10 9 Dereferencing a Pointer The real benefit of pointers is that they allow us to indirectly access and modify the variables they point to Using a pointer to obtain the value to which it points is called dereferencing the pointer Chapter10 10 5

The Indirection Operator Dereferencing is accomplished by using the indirection (or dereferencing) operator, * (this is different than the * used in declarations) Example: *ptr is the value pointed to by ptr *ptr is the value that is found at the address stored in ptr Chapter10 11 The Indirection Operator The address operator * ptr (&) = and =? the indirection operator (*) are inverses &* ptr = =? Let s look at what that means: int x = 15; *&x = =? int *ptr = &x; 15 ptr 15 Chapter10 12 6

Indirection Operator Program #include <iostream> using namespace std; int main ( void ) { int X = 25; int *Ptr; Here is the value in X, printed twice: 25 25 Once again, here is the value in X: 100 100 Ptr = &X; // Store the address of X in Ptr cout << "Here is the value in X, printed twice:\n"; cout << X << " " << *Ptr << endl; *Ptr = 100; cout << "Once again, here is the value in X:\n"; cout << X << " " << *Ptr << endl; return 0; } // main Chapter10 13 Using Pointers Program #include <iostream> using namespace std; int main ( void ) { int X = 25, Y = 50, Z = 75; int *Ptr; cout << "Here are the values of X, Y, and Z:\n"; cout << X << " " << Y << " " << Z << endl; Ptr = &X; *Ptr *= 2; Ptr = &Y; *Ptr *= 2; Ptr = &Z; *Ptr *= 2; Here are the values of X, Y, and Z: 25 50 75 Once again, here are the values of X, Y, and Z: 50 100 150 cout << "Once again, here are the values of X, Y, and Z:\n"; cout << X << " " << Y << " " << Z << endl; return 0; } // main Chapter10 14 7

Relationship Between Arrays and Pointers Recall: an array name, without brackets or subscript, represents the starting address of the array That means that array names can be used as pointers, and vice-versa As we ve already seen, the address a pointer points to can be changed However, the address an array name points to cannot be changed, because an array name is a constant pointer Chapter10 15 Basic Pointer Arithmetic For declaration short Numbers [5]; Chapter10 16 8

Basics of Pointer Arithmetic short Numbers [5]; Notice that adding 1 to the pointer Numbers, actually adds 1 multiplied by the size of the variable type *(Numbers+1)is really *(Numbers + 1 * sizeof(short)) If Numbers was declared as double, *(Numbers+2) is really *(Numbers + 2 * sizeof(double)) Chapter10 17 Basics of Pointer Arithmetic *arrayname (same as *(arrayname + 0) refers to arrayname[0] *(arrayname + 1) refers to arrayname[1] *(arrayname + 2) refers to arrayname[2] etc. In general *(arrayname + index) is equivalent to arrayname[index] Be careful not to give a pointer an address outside the bounds of the array. Chapter10 18 9

Arrays and Pointers Program The first element of the array is 10 #include <iostream> The third element of the array is 30 using namespace std; int main ( void ) { short Numbers[] = {10, 20, 30, 40, 50}; cout << "The first element of the array is "; cout << *Numbers << endl; cout << "The third element of the array is "; cout << *(Numbers + 2) << endl; return 0; } // main Chapter10 19 Pointers and Arrays Program #include <iostream> using namespace std; int main ( void ) { int Numbers[5]; Enter five numbers: 5 10 15 20 25 [Enter] Here are the numbers you entered: 5 10 15 20 25 cout << "Enter five numbers: "; for ( int Count = 0; Count < 5; Count++ ) cin >> *( Numbers + Count ); cout << "Here are the numbers you entered:\n"; for ( int Count = 0; Count < 5; Count++ ) cout << *( Numbers + Count )<< " "; cout << endl; return 0; } // main Chapter10 20 10

Difference Between Array Names and Pointers Given float x[20], y[20]; float * fptr; which of the following are legal? fptr = x; legal fptr = y; legal x = y; illegal y = fptr; illegal Chapter10 21 More Pointers and Arrays Program #include <iostream> #include <iomanip> using namespace std; int main ( void ) { float Coins[5] = { 0.05, 0.1, 0.25, 0.5, 1.0 }; float *FloatPtr; // Pointer to a float int Count; // Array index FloatPtr = Coins; // FloatPtr now points to Coins array cout << setprecision(2) << fixed; cout << "Here are the values in the Coins array:\n"; Chapter10 22 11

More P and A Program for ( Count = 0; Count < 5; Count++ ) cout << FloatPtr[Count] << " "; cout << "\nand here they are again:\n"; for ( Count = 0; Count < 5; Count++ ) cout << *(Coins + Count) << " "; cout << endl; return 0; } // main Here are the values in the Coins array: 0.05 0.10 0.25 0.50 1.00 And here they are again: 0.05 0.10 0.25 0.50 1.00 Chapter10 23 Elements of the Array Program #include <iostream> #include <iomanip> using namespace std; int main ( void ) { float Coins[5] = { 0.05, 0.1, 0.25, 0.5, 1.0 }; float *FloatPtr; // Pointer to a float int Count; // Array index cout << setprecision(2); cout << "Here are the values in the Coins array:\n"; Chapter10 24 12

Elements of the Array Program (cont.) for ( Count = 0; Count < 5; Count++ ) { FloatPtr = &Coins[Count]; cout << *FloatPtr << " "; } // for cout << endl; } // main Here are the values in the Coins array: 0.05 0.10 0.25 0.50 1 Chapter10 25 Pointer Arithmetic Some mathematical operations may be performed on pointers The ++ and -- operators may be used to increment or decrement a pointer variable An integer may be added to or subtracted from a pointer variable. This may be performed with the +, -, +=, or -= operators A pointer may be subtracted from another pointer Chapter10 26 13

Pointer Arithmetic Program #include <iostream> using namespace std; int main ( void ) { int Set[8] = { 5, 10, 15, 20, 25, 30, 35, 40 }; int *Nums, Index; Nums = Set; cout << "The numbers in Set are:\n"; for ( Index = 0; Index < 8; Index++, Nums++ ) cout << *Nums << " "; cout << "\nthe numbers in Set backwards are:\n"; for ( Index = 0; Index < 8; Index++ ) { Nums--; cout << *Nums << " "; } // for return 0; } // main The numbers in Set are: 5 10 15 20 25 30 35 40 The numbers in Set backwards are: 40 35 30 25 20 15 10 5 Chapter10 27 Initializing Pointers Pointers may be initialized with the address of an existing object Chapter10 28 14

Which are valid? int one; int *ptr = one; float two, *ptr = &two; int ages[20]; int *pt_arr = ages; int *ptr = &three; int three; float two; int *ptr = &two; double *ptr; *ptr = 3.14159; Chapter10 29 Comparing Pointers If one address comes before another address in memory, the first address is considered less than the second C++ s relational operators maybe used to compare pointer values Chapter10 30 15

Addresses in an Array Chapter10 31 Comparing Pointers Program #include <iostream> using namespace std; int main ( void ) { int Set[8] = { 5, 10, 15, 20, 25, 30, 35, 40 }; int *Nums = Set; cout << "The numbers in Set are:\n"; cout << *Nums << " "; for ( ; Nums < &Set[7]; ) { Nums++; cout << *Nums << " "; } // for Chapter10 32 16

Comparing Pointers Program cout << "\nthe numbers in Set backwards are:\n"; cout << *Nums << " "; for ( ; Nums > Set; ) { Nums--; cout << *Nums << " "; } // for return 0; } // main The numbers in set are: 5 10 15 20 25 30 35 40 The numbers in set backwards are: 40 35 30 25 20 15 10 5 Chapter10 33 Pointers as Function Parameters A pointer can be used as a function parameter It gives the function access to the original argument, much like a reference parameter does Chapter10 34 17

Pointer as Parameters Program #include <iostream> using namespace std; void GetNumber ( int * ); void DoubleValue ( int * ); Enter an integer number: 10 [Enter] That value doubled is 20 int main ( void ) { int Number; GetNumber( &Number ); DoubleValue( &Number ); cout << "That value doubled is " << Number << endl; return 0; } // main Chapter10 35 Pointers as Parameters Program // call is GetNumber(&Number); void GetNumber ( int *Input ) { cout << "Enter an integer number: "; cin >> *Input; } // GetNumber // call is DoubleValue(&Number); void DoubleValue ( int *Val ) { *Val *= 2; } // DoubleValue Chapter10 36 18

More Pointers and Parameters - Program #include <iostream> #include <iomanip> using namespace std; void GetSales ( float * ); float TotalSales ( float * ); int main ( void ) { float Sales[4]; GetSales( Sales ); cout << setprecision(2); Chapter10 37 More Pointers and Parameters Program cout << fixed << showpoint; cout << "The total sales for the year are $"; cout << TotalSales(Sales) << endl; return 0; } // main void GetSales ( float *Array ) { for ( int Count = 0; Count < 4; Count++ ) { cout << "Enter the sales figure for quarter "; cout << (Count + 1) << ": "; cin >> Array[Count]; } // for } // GetSales Chapter10 38 19

More Pointers and Parameters Program float TotalSales ( float *Array ) { float Sum = 0.0; for ( int Count = 0; Count < 4; Count++ ) { Sum += *Array; Array++; } // for return Sum; } // TotalSales Chapter10 39 More Pointers and Parameters Output Enter the sales figure for quarter 1: 10263.98 [Enter] Enter the sales figure for quarter 2: 12369.69 [Enter] Enter the sales figure for quarter 3: 11542.13 [Enter] Enter the sales figure for quarter 4: 14792.06 [Enter] The total sales for the year are $48967.86 Chapter10 40 20

Dynamic Memory Allocation Dynamic Memory Allocation: allowing the program to create its own variables on the fly With dynamic memory allocation, variables may be created and destroyed while a program is running A program, while running, asks the computer to set aside a chunk of unused memory large enough to hold a variable of a specific data type Chapter10 41 Dynamic Memory Allocation: the new Operator Use the new operator to dynamically allocate memory int * iptr; iptr = new int; //iptr contains address of //newly allocated memory *iptr = 25; // assigns 25 to the new variable Chapter10 42 21

Dynamic Memory Allocation of an Array Use the new operator to dynamically allocate memory for an array int * iptr; iptr= new int[100]; // iptr contains address of the first element of newly // allocated array of 100 elements for (int count=0; count<100; count++) iptr[count]=0; // using array notation to initialize values of the new // array to 0 Chapter10 43 Dynamic Memory Allocation: the Null Pointer When memory cannot be dynamically allocated, the new operator returns the address 0 or NULL (NULL is a named constant in iostream) A pointer that contains the address 0 is called a null pointer A program that uses dynamically allocated memory should always check to see if the new operator returns NULL Chapter10 44 22

Checking for the Null Pointer int * iptr; iptr = new int[100]; if ( iptr == NULL ) { cout << "Memory Allocation Error\n"; return; } // if Chapter10 45 Dynamic Memory Allocation the delete Operator Use the delete operator to dynamically deallocate memory that was previously allocated using new. For a single variable delete iptr; for an array delete [ ] iptr; Chapter10 46 23

Dynamic Arrays Program #include <iostream> #include <iomanip> using namespace std; int main ( void ) { float *Sales, Total = 0, Average; int NumDays; cout << "How many days of sales figures do you wish "; cout << "to process? "; cin >> NumDays; Sales = new float [NumDays]; Chapter10 47 Dynamic Arrays Program if ( Sales == NULL ) { cout << "Error allocating memory!\n"; return; } // if // Get sales cout << "Enter the sales figures below.\n"; for ( int Count = 0; Count < NumDays; Count++ ) { cout << "Day " << (Count + 1) << ": "; cin >> Sales[Count]; } // for // Calculate the total sales for ( Count = 0; Count < NumDays; Count++ ) Total += Sales[Count]; Chapter10 48 24

Dynamic Arrays Program // Calculate the average sales per day Average = Total / NumDays; // Display the results cout << fixed << showpoint << setprecision(2); cout << "\n\ntotal Sales: $" << Total << endl; cout << "Average Sales: $" << Average << endl; delete [] Sales; return 0; } // main Chapter10 49 Dynamic Arrays Output How many days of sales figures do you wish to process? 5 [Enter] Enter the sales figures below. Day 1: 898.63 [Enter] Day 2: 652.32 [Enter] Day 3: 741.85 [Enter] Day 4: 852.96 [Enter] Day 5: 921.37 [Enter] Total Sales: $4067.13 Average Sales: $813.43 Chapter10 50 25

Returning Pointers from Functions Functions can return pointers, but you must be sure the object the pointer references still exists You should only return a pointer from a function if it is: A pointer to an object that was passed into the function as an argument A pointer to a dynamically allocated object Chapter10 51 Returning Pointers from Functions Given the function: char *getname() { char name[81]; cout << "Enter your name: "; cin.getline( name, 81 ); return name; } // getname Will this work? Returns a pointer to a local variable. What happens to local variables when the function returns? Chapter10 52 26

Returning Pointers from Functions How about this one? // main function char *title, *name; title = getname( name ); char *getname(char *name) { cout << "Enter your name: "; cin.getline( name, 81 ); return name; } // getname Chapter10 53 Returning Pointers from Functions And this one? char *getname() { char *name; name = new char[81]; cout << "Enter your name: "; cin.getline( name, 81 ); return name; } // getname Chapter10 54 27