Lecture 23: Pointer Arithmetic

Similar documents
C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

What is an algorithm?

Week 3: Pointers (Part 2)

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

BITG 1113: Function (Part 2) LECTURE 5

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

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.

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

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

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):

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CSC 211 Intermediate Programming. Arrays & Pointers

Lecture 21 Tao Wang 1

BITG 1113: POINTER LECTURE 12

Name Section: M/W or T/TH. True or False (14 Points)

Pointers. Variable Declaration. Chapter 10

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

University of Swaziland Department OfComputer Science Main Examination December 2016

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

Introduction to Computer Science Midterm 3 Fall, Points

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

W3101: Programming Languages C++ Ramana Isukapalli

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

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

by Pearson Education, Inc. All Rights Reserved.

A First Program - Greeting.cpp

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

Exam 3 Chapters 7 & 9

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

C++ For Science and Engineering Lecture 12

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

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.

True or False (15 Points)

Module 07: Programming in C++

Chapter 2: Basic Elements of C++

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

CSCE 110 PROGRAMMING FUNDAMENTALS

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

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Lecture 05 POINTERS 1

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

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

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

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Functions, Arrays & Structs

Reference operator (&)

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

C++ For Science and Engineering Lecture 15

Lab 8 The Stack (LIFO) Structure

Computer Programming

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Object oriented programming

EECS402 Lecture 02. Functions. Function Prototype

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Homework #3 CS2255 Fall 2012

Templates and Vectors

Name SECTION: 12:45 2:20. True or False (12 Points)

CS2255 HOMEWORK #1 Fall 2012

Pointers, Dynamic Data, and Reference Types

True or False (12 Points)

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

Object-Oriented Programming for Scientific Computing

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Introduction to C ++

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

True or False (15 Points)

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

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

List Iterator Implementation

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Pointers. 10/5/07 Pointers 1

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Arrays. Week 4. Assylbek Jumagaliyev

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

Using Parallel Arrays. Parallel Array Example

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

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

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

CSCE Practice Midterm. Data Types

Exercise 1.1 Hello world

Lab Instructor : Jean Lai

Chapter 17 - Notes Recursion

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

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

TEMPLATE IN C++ Function Templates

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

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

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

Pointers! Arizona State University 1

CPSC 427a: Object-Oriented Programming

Lecture on pointers, references, and arrays and vectors

Lecture 8 Tao Wang 1

Transcription:

Lecture 23: Pointer Arithmetic Wai L. Khoo Department of Computer Science City College of New York November 29, 2011 Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 1 / 14

Pointer Arithmetic Pointer Arithmetic Pointer variables, like all variables, contain values The value stored in a pointer is a memory address By adding or subtracting numbers to pointers you can obtain different addresses Pointer values can be compared using relational operators (==, <, >, etc.) Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 2 / 14

Pointer Arithmetic Pointer Arithmetic Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 3 / 14

Pointer Arithmetic Pointer Arithmetic Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 3 / 14

Pointer Arithmetic Pointer Arithmetic Increment and decrement operators can be applied as both prefix and postfix operators *ptnum++ // use the pointer and then increment it *++ptnum // increment the pointer before using it *ptnum // use the pointer and then decrement it * ptnum // decrement the pointer before using it Of the four possible forms, *ptnum++ is most common Allows accessing each array element as the address is marched along from starting address to address of last array element Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 4 / 14

Pointer Arithmetic Sample Program - Pointer Arithmetic #include <iostream> using namespace std; Code int main() { const int NUMS = 5; int nums[nums] = {16, 54, 7, 43, -5}; int i, total = 0, *npt; npt = nums; // store address of nums[0] in npt for (i = 0; i < NUMS; i++) total = total + *npt++; cout << "The total of the array elements is " << total << endl; } return 0; Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 5 / 14

Pointer Arithmetic Sample Program - Pointer Arithmetic 2 #include <iostream> using namespace std; Code int main() { const int NUMS = 5; int nums[nums] = {16, 54, 7, 43, -5}; int total = 0, *npt; npt = nums; // store address of nums[0] in npt while (npt < nums + NUMS) total += *npt++; cout << "The total of the array elements is " << total << endl; } return 0; Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 6 / 14

Pointer Arithmetic Pointer Initialization Pointers can be initialized with they are declared int *ptnum = &miles; Pointers to arrays can also be initialized when they are declared double *zing = &volts[0]; Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 7 / 14

Passing Addresses Passing Addresses Reference pointers can be used to pass addresses through reference parameters Implied use of an address Pointers can be used explicitly to pass addresses with references Explicitly passing references with the address operator is called pass by reference Called function can reference, or access, variables in the calling function by using the passed addresses Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 8 / 14

Passing Addresses Explicitly Passing Addresses Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 9 / 14

Passing Addresses Sample Program - Passing Addresses #include <iostream> using namespace std; void swap(double *, double *); Code // function prototype int main() { double firstnum = 20.5, secnum = 6.25; swap(&firstnum, &secnum); // call swap } return 0; // this function illustrates passing pointer arguments void swap(double *nm1addr, double *nm2addr) { cout << "The number whose address is in nm1addr is " << *nm1addr << endl; cout << "The number whose address is in nm2addr is " << *nm2addr << endl; } return; Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 10 / 14

Passing Addresses Sample Program - Passing Addresses 2 #include <iostream> using namespace std; Code void swap(double *, double *); // function prototype int main() { double firstnum = 20.5, secnum = 6.25; cout << "The value stored in firstnum is: " << firstnum << endl; cout << "The value stored in secnum is: " << secnum << "\n\n"; swap(&firstnum, &secnum); // call swap cout << "The value stored in firstnum is now: " << firstnum << endl; cout << "The value stored in secnum is now: " << secnum << endl; } return 0; // this function swaps the values in its two arguments void swap(double *nm1addr, double *nm2addr) { double temp; temp = *nm1addr; // save firstnum s value *nm1addr = *nm2addr; // move secnum s value into firstnum *nm2addr = temp; // change secnum s value return; } Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 11 / 14

Common Programming Errors Common Programming Errors Incorrectly applying address and indirection operators Taking addresses of pointer constants Initialized pointer variables incorrectly Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 12 / 14

Summary Summary Any access to an array element with subscript notation can always be replaced with pointer notation Arrays are passed to functions as addresses Pointers can be incremented, decremented, compared, and assigned Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 13 / 14

After class... Summary Read Chapter 13.1-13.3 Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 14 / 14