CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers

Similar documents
CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

CS103 Unit 6 - Pointers. Mark Redekopp

CS103 Unit 6 - Pointers. Mark Redekopp

Homework #3 CS2255 Fall 2012

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

[0569] p 0318 garbage

Pointer Data Type and Pointer Variables

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

a data type is Types

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

CSC 211 Intermediate Programming. Pointers

Pointers, Dynamic Data, and Reference Types

CMSC202 Computer Science II for Majors

C++ for Java Programmers

C Pointers. 7.2 Pointer Variable Definitions and Initialization

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

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

Pointers, Arrays and C-Strings

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

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

BITG 1113: POINTER LECTURE 12

CS201- Introduction to Programming Current Quizzes

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

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

CSCI 104 Memory Allocation. Mark Redekopp David Kempe

What is an algorithm?

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Fundamentals of Programming Session 20

Variation of Pointers

CS31 Discussion 1E Spring 17 : week 08

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

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

Lecture 05 POINTERS 1

Introduction to Computer Science Midterm 3 Fall, Points

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Dynamic Memory Allocation

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

POINTER & REFERENCE VARIABLES

Pointers. 10/5/07 Pointers 1

PROGRAMMAZIONE I A.A. 2017/2018

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Intermediate Programming, Spring 2017*

CS 2461: Computer Architecture I

Lab 3. Pointers Programming Lab (Using C) XU Silei

Week 3: Pointers (Part 2)

MYcsvtu Notes LECTURE 34. POINTERS

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

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

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

C++ for Java Programmers


SYSC 2006 C Winter 2012

Pointers in C/C++ 1 Memory Addresses 2

Basic Pointers. CSCI 112: Programming in C

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

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

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

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

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

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

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Chapter 9. Pointers and Dynamic Arrays

Lecture 04 Introduction to pointers

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

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

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

Goals of this Lecture

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

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Assist. Prof. Dr. Caner ÖZCAN

Chapter 11: Pointers

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

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

Pointers. Variable Declaration. Chapter 10

Memory and C++ Pointers

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

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

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

Pointers II. Class 31

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Fundamentals of Programming Session 19

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

by Pearson Education, Inc. All Rights Reserved.

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

CS102 Software Engineering Principles

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

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

CS 61c: Great Ideas in Computer Architecture

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

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

ECE 15B COMPUTER ORGANIZATION

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

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

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

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

Transcription:

CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 6: Pointers

Pointers Pointers are references to other things Pointers are the address of some other variable in memory things can be data (i.e. int s, char s, double s) or other pointers A pointer of any type is a 32-bit number which should be interpreted as an address Pointer variables store a location of something stored elsewhere Rather than refer to a value directly, we can refer to it via its address 20bc4 1298 20bc8 a 20bcc 5.375 20bd0 107 20bd4 43 20bd8 00 20bdc 00 20be0 00 Addresses in Hex

Pointers in everyday life The concept of a pointer is very common and used in many places in everyday life Phone numbers, e-mail or mailing addresses are references or pointers to you or where you live Excel workbook has cell names we can use to reference the data ( =$A$1 means get data in $A$1) URL s (www.usc.edu is a pointer to a physical HTML file) and can be used in any other page to point to USC s website

Why use Pointers? Share access to common data (hold onto one copy, everybody points to it) Flexibility (dynamic data structures) Precisely control of memory allocation/ deallocation ourselves

5 Declaring Pointer Variables Syntax: Examples: int *p; char *ch; These statements are equivalent: int *p; int* p; int * p; C++ Programming: Program Design Including Data Structures, Sixth Edition

6 Declaring Pointer Variables In the statement: int* p, q; Only p is a pointer variable q is an int variable To avoid confusion, abach the character * to the variable name: int *p, q; int *p, *q; C++ Programming: Program Design Including Data Structures, Sixth Edition

7 Address of Operator (&) Address of operator (&): A unary operator that returns the address of its operand Example: int x; int *p; p = &x; Assigns the address of x to p C++ Programming: Program Design Including Data Structures, Sixth Edition

Pointers & operator yields address of a variable in C++ (Tip: Read &foo as address of foo ) int x = 1298; char y= a ; float z = 5.375; int dat[2] = {10,5}; &x =>??, &y =>??, &z =>??, &dat[1] =??; dat =>?? 20bc4 20bc8 20bcc 20bd0 20bd4 20bd8 20bdc 20be0 1298 a 5.375 10 5 00 00 00 x y z dat[0] dat[1]

Pointers & operator yields address of a variable in C++ (Tip: Read &foo as address of foo ) int x = 1298; char y= a ; float z = 5.375; int dat[2] = {10,5}; &x => 0x20bc4, &y => 0x20bc8, &z => 0x20bcc, &dat[1] => 0x20bd4; dat => 0x20bd0 20bc4 20bc8 20bcc 20bd0 20bd4 20bd8 20bdc 20be0 1298 a 5.375 10 5 00 00 00 x y z dat[0] dat[1] Number of bits used for an address depends on OS, etc. 32-bit OS => 32-bit addresses 64-bit OS => 64-bit addresses

10 Dereferencing Operator (*) Dereferencing operator (or indirection operator): When used as a unary operator, * refers to object to which its operand points Example: cout << *p << endl; Prints the value stored in the memory location pointed to by p C++ Programming: Program Design Including Data Structures, Sixth Edition

Pointers A pointer variable is a location in memory that is dedicated to storing an address to a certain type of data Variable means it can be changedat first it can hold the address of data item 1 then it can be updated to hold the address of data item 2 Requires 4-bytes of storage in a 32-bit system or 8-bytes in a 64-bit systems Use a * after the type to indicate this a pointer variable to that type of data Declare variables: 20bc4 1298 x int x = 1298; char y= a ; 20bc8 a y double z = 5.375; 20bcc 5.375 z int dat[2] = {10,5}; 20bd0 10 dat[0] int *ptr1; 20bd4 5 dat[1] ptr1 = &x; 20bd8 ptr1 ptr1 = &dat[0]; 20bdc ptr2 float *ptr2; 20be0 00 ptr2 = &z;

Pointers A pointer variable is a location in memory that is dedicated to storing an address to a certain type of data Variable means it can be changedat first it can hold the address of data item 1 then it can be updated to hold the address of data item 2 Requires 4-bytes of storage in a 32-bit system or 8-bytes in a 64-bit systems Use a * after the type to indicate this a pointer variable to that type of data Declare variables: int x = 1298; char y= a ; double z = 5.375; int dat[2] = {107,43}; int *ptr1; ptr1 = &x; ptr1 = &dat[0]; float *ptr2; ptr2 = &z; // ptr1 = 0x20bc4 // ptr1 = 0x20bd0 // ptr2 = 0x20bcc 20bc4 20bc8 20bcc 20bd0 20bd4 20bd8 20bdc 20be0 1298 a 5.375 107 43 20bc4 20bd0 20bcc 00 x y z dat[0] dat[1] ptr1 ptr2

De-referencing / Indirection Once a pointer has been written with an address of some other object, we can use it to access that object (i.e. dereference the pointer) using the * operator Read *foo as value pointed to by foo value at the address stored in foo (not value of foo or value of address of foo ) Using URL analogy, using the * operator on a pointer is like clicking on a URL Examples: int a = 5; a = a + *ptr1; (*ptr1)++; *ptr2 = *ptr1 - *ptr2; ; 20bc4 20bc8 20bcc 20bd0 20bd4 20bd8 20bdc 20be0 1298 a 5.375 10 5 20bd0 20bcc 00 x y z dat[0] dat[1] ptr1 ptr2

De-referencing / Indirection Once a pointer has been written with an address of some other object, we can use it to access that object (i.e. dereference the pointer) using the * operator Read *foo as value pointed to by foo value at the address stored in foo (not value of foo or value of address of foo ) Using URL analogy, using the * operator on a pointer is like clicking on a URL Examples: int a = 5; a = a + *ptr1; // a = 15 after exec. (*ptr1)++; // dat[0] = 11 *ptr2 = *ptr1 - *ptr2; // z = 11 5.375 = 5.625 * in a type declaration = declare/allocate a pointer * in an expression/assignment = dereference 20bc4 1298 20bc8 a 20bcc 5.375 20bd0 10 20bd4 5 20bd8 20bd0 20bdc 20bcc 20be0 00 x y z dat[0] dat[1] ptr1 ptr2

Declaring vs Dereferencing Declaring a pointer int *ptr1; float *ptr2 = &z; Assignment ptr1 = dat; Dereferencing a pointer int a = 5; "a = a + *ptr1;" 20bc4 1298 20bc8 a 20bcc 5.375 20bd0 10 20bd4 5 20bd8 20bd0 20bdc 20bcc 20be0 00 x y z dat[0] dat[1] ptr1 ptr2

Pointer Arithmetic Pointers are variables storing addresses Addresses are just numbers We can perform addition or subtraction on those pointer variables (i.e. addresses) just like any other variable The number added/subtracted is implicitly multiplied by the size of the object int *ptr = &x; ptr = ptr + 1; // address in ptr was incremented by 4 Examples: (*ptr1)++; // dat[0] = 11 ptr1++; x=*ptr1++; // ptr1 now points at dat[1] // x = dat[1] = 5 then inc. ptr1 // to 0x20bd8 (*(ptr1-2))++; // dat[0] = 12 20bc4 1298 20bc8 a 20bcc 5.375 20bd0 11 20bd4 5 20bd8 20bd0 20bdc 20bcc 20be0 00 x y z dat[0] dat[1] ptr1 ptr2

17 OperaGons on Pointer Variables Pointer arithmegc can be very dangerous: Program can accidentally access memory locagons of other variables and change their content without warning Some systems might terminate the program with an appropriate error message Always exercise extra care when doing pointer arithmegc C++ Programming: Program Design Including Data Structures, Sixth Edition

Pointers to Pointers to Pointers can point to other pointers Essentially a chain of links Example int k,x[3] = {105, 107, 109}; int *myptr = x; int **ourptr = &myptr; k = *myptr; k = (**ourptr) + 1; k = *(*ourptr + 1); 20bc4 105 20bc8 107 20bcc 109 20bd0 20bd4 20bd8 00 20bdc 00 20be0 00 k X[0] X[1] X[2] myptr ourptr

Pointers to Pointers to Pointers can point to other pointers Essentially a chain of links Example int k,x[3] = {105, 107, 109}; int *myptr = x; int **ourptr = &myptr; k = *myptr; // k = 105 k = (**ourptr) + 1; // k = 106 k = *(*ourptr + 1); // k = 107 20bc4 105 20bc8 107 20bcc 109 20bd0 20bc4 20bd4 20bd0 20bd8 00 20bdc 00 20be0 00 k X[0] X[1] X[2] myptr ourptr