What is an algorithm?

Similar documents
Pointers! Arizona State University 1

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

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.

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

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

Homework #3 CS2255 Fall 2012

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

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

Exam 3 Chapters 7 & 9

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

Lecture 23: Pointer Arithmetic

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

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

Pointers. 10/5/07 Pointers 1

CSC 211 Intermediate Programming. Arrays & Pointers

Week 3: Pointers (Part 2)

Introduction to Computer Science Midterm 3 Fall, Points

by Pearson Education, Inc. All Rights Reserved.

Pointers, Dynamic Data, and Reference Types

Fundamentals of Programming Session 20

C++ Programming Chapter 7 Pointers

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Lecture 05 POINTERS 1

C Pointers. 6th April 2017 Giulio Picierro

Programming for Engineers Pointers

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Pointers and Dynamic Memory Allocation

[0569] p 0318 garbage

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Pointers. Variable Declaration. Chapter 10

Chapter 9. Pointers and Dynamic Arrays

MYcsvtu Notes LECTURE 34. POINTERS

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

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

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

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

CS201- Introduction to Programming Current Quizzes

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

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


Variation of Pointers

Structured Data. CIS 15 : Spring 2007

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

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

CMSC202 Computer Science II for Majors

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

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Introduction to Scientific Computing and Problem Solving

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

C++ for Java Programmers

CS2255 HOMEWORK #1 Fall 2012

Pointers in C/C++ 1 Memory Addresses 2

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

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

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.

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

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Solution: A pointer is a variable that holds the address of another object (data item) rather than a value.

Pointers. Pointers. Pointers (cont) CS 217

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

Structures and Pointers

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Assist. Prof. Dr. Caner ÖZCAN

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

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

Language comparison. C has pointers. Java has references. C++ has pointers and references

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

BITG 1113: POINTER LECTURE 12

Pointers and Dynamic Arrays

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

Object oriented programming C++

UNIT- 3 Introduction to C++

Arrays and Pointers in C. Alan L. Cox

a data type is Types

Arrays. Week 4. Assylbek Jumagaliyev

(7-1) Modular Programming H&K Chapter 6. Instructor - Andrew S. O Fallon CptS 121 (February 21, 2018) Washington State University

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

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

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

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021

SYSC 2006 C Winter 2012

CS201 Latest Solved MCQs

Computer Programming

C Pointers. 7.2 Pointer Variable Definitions and Initialization

CHAPTER 4 FUNCTIONS. 4.1 Introduction

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

The C++ Language. Arizona State University 1

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

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Goals of this Lecture

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

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

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

Fundamentals of Programming Session 20

Pointers & Dynamic Arrays

Transcription:

Announcements CS 142 C++ Pointers Reminder Program 6 due Sunday, Nov. 9 th by 11:55pm 11/3/2014 2 Pointers and the Address Operator Pointer Variables Each variable in a program is stored at a unique address in memory Use the address operator & to get the address of a variable: int num = -23; cout << &num; // prints address // in hexadecimal The address of a memory location is a pointer Assignment: int num = 25; int *intptr; intptr = &num; Memory layout: num intptr 25 0x4a00 address of num: 0x4a00 Can access num using intptr and indirection operator *: cout << intptr; // prints 0x4a00 cout << *intptr; // prints 25 11/3/2014 3 11/3/2014 4 1

Relationship What is Between an algorithm? Arrays and Pointers Array name is starting address of array int vals[] = {4, 7, 11}; starting address of vals: 0x4a00 cout << vals; // displays 0x4a00 cout << vals[0]; // displays 4 Arrays & Pointers Array name can be used as a pointer constant int vals[] = {4, 7, 11}; cout << *vals; // displays 4 Pointer can be used as an array name cout << valptr[1]; // displays 7 5 6 Pointers in Expressions Given: int vals[]={4,7,11}; What is valptr + 1? It means (address in valptr) + (1 * size of an int) cout << *(valptr+1); // displays 7 cout << *(valptr+2); // displays 11 Must use ( ) in expression Array Access Array elements can be accessed in many ways 7 8 2

Array Access Pointer Arithmetic Array notation vals[i] is equivalent to the pointer notation *(vals + i) No bounds checking performed on array access Some arithmetic operators can be used with pointers: Increment and decrement operators ++, -- Integers can be added to or subtracted from pointers using the operators +, -, +=, and -= One pointer can be subtracted from another by using the subtraction operator - 9 10 Pointer Arithmetic Assume that array int v[5] has been declared and that its first element is at memory location 3000. Pointer Arithmetic Assume the variable definitions int vals[]={4,7,11}; Examples of use of ++ and -- valptr++; // points at 7 valptr--; // now points at 4 Array v and a pointer variable int *vptr that points to v. Pointer vptr after pointer arithmetic. 11 12 3

More on Pointer Arithmetic Pointers as Function Parameters Assume the variable definitions: int vals[]={4,7,11}; Example of the use of + to add an int to a pointer: cout << *(valptr + 2); // prints 11 Example of use of +=: valptr = vals; // points at 4 valptr += 2; // points at 11 A pointer can be a parameter Works like a reference parameter to allow change to argument from within function A pointer parameter must be explicitly dereferenced to access the contents at that address Example of pointer subtraction valptr += 2; cout << valptr - val; // prints 2; the number // of ints between valptr // and val. 13 14 Pointers as Function Parameters Requires: 1) asterisk * on parameter in prototype and heading void getnum(int *ptr); 2) asterisk * in body to dereference the pointer cin >> *ptr; 3) address as argument to the function getnum(&num); Pointers as Function Parameters void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int num1 = 2, num2 = -3; swap(&num1, &num2); 15 16 4

Returning Pointers from Functions Pointers to Constants Pointer can be return type of function int* newnum(); Function must not return a pointer to a local variable in the function Function should only return a pointer to data that was passed to the function as an argument to dynamically allocated memory (we ll talk about dynamically allocated memory next week) Pointer to a constant: cannot change the value that is pointed at Must use const keyword in pointer definition: const double taxrates[] = {0.65, 0.8, 0.75}; const double *rateptr; Use const keyword for pointers in function headers to protect data from modification within function 17 18 Constant Pointer Constant pointer: address in pointer cannot change once pointer is initialized Defined with const keyword adjacent to variable name: int classsize = 24; int * const classptr = &classsize; Must be initialized when defined Can be used without initialization as a function parameter Initialized by argument when function is called Function can receive different arguments on different calls While the address in the pointer cannot change, the data at that address may be changed Arrays of Pointers The const char * portion of the declaration indicates that each element of array suit is of type pointer to char constant data. 19 20 5

Demo Public directory under C++ -> pointers pointers2.cpp //examples of more pointers arrayswithpointers.cpp swap.cpp //different function calls with pointers 21 6