CMSC202 Computer Science II for Majors Lecture 05 – References Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

CSCI 171 Presentation 11 Pointers. Pointer Basics.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
CMSC202 Computer Science II for Majors Lecture 06 – Classes and Objects Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC202 Computer Science II for Majors Lecture 09 – Overloaded Operators and More Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Pointers and Classes.
Constructors and Destructors
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
ECE Application Programming
User-Written Functions
UMBC CMSC 104 – Section 01, Fall 2016
Computer Skills2 for Scientific Colleges
Examples of Classes & Objects
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Lecture-5 Arrays.
Learning Objectives Pointers Pointer in function call
Strings A string is a sequence of characters treated as a group
Arrays in C.
Primitive Types Vs. Reference Types, Strings, Enumerations
C Passing arrays to a Function
7 Arrays.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Classes & Objects: Examples
Pointers and Pointer-Based Strings
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Lecture 18 Arrays and Pointer Arithmetic
Pointers Call-by-Reference CSCI 230
Computer Skills2 for Scientific Colleges
Constructors and Destructors
Pointers Kingdom of Saudi Arabia
Classes and Objects.
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
CS111 Computer Programming
Homework Starting K&R Chapter 5 Good tutorial on pointers
Chapter 7: User-Defined Functions II
CISC181 Introduction to Computer Science Dr
CMSC202 Computer Science II for Majors Lecture 03 – Arrays and Functions Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
7 Arrays.
ECE Application Programming
C++ Programming Lecture 17 Pointers – Part I
CS150 Introduction to Computer Science 1
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Arrays Arrays A few types Structures of related data items
Data Structures and Algorithms Introduction to Pointers
Submitted By : Veenu Saini Lecturer (IT)
EECE.2160 ECE Application Programming
Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
C++ Programming Lecture 18 Pointers – Part II
Pointers and dynamic objects
Functions Reasons Concepts Passing arguments to a function
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
CISC181 Introduction to Computer Science Dr
EECE.2160 ECE Application Programming
Pointers, Dynamic Data, and Reference Types
Programming fundamentals 2 Chapter 3:Pointer
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CMSC202 Computer Science II for Majors Lecture 05 – References Dr. Katherine Gibson Based on slides by Chris Marron at UMBC

Last Class We Covered Variables – Values – Addresses Pointers – Creating – Initializing – Dereferencing Pointers and Functions – “Returning” more than one value 2

Any Questions from Last Time?

Today’s Objectives To review and better understand pointers To discuss how pointers are used to pass entire arrays to functions To learn about references 4

Review of Pointers

Visualization of Pointers 6 int x = 5; int *xPtr = &x; /* xPtr points to x */ int y = *xPtr; /* y’s value is ? */ variable name xxPtry memory address 0x7f96c0x7f9600x7f95c value 50x7f96c?

Visualization of Pointers 7 int x = 5; int *xPtr = &x; /* xPtr points to x */ int y = *xPtr; /* y’s value is ? */ variable name xxPtry memory address 0x7f96c0x7f9600x7f95c value 50x7f96c?

Visualization of Pointers 8 int x = 5; int *xPtr = &x; /* xPtr points to x */ int y = *xPtr; /* y’s value is ? */ variable name xxPtry memory address 0x7f96c0x7f9600x7f95c value 50x7f96c?

Visualization of Pointers 9 int x = 5; int *xPtr = &x; /* xPtr points to x */ int y = *xPtr; /* y’s value is ? */ variable name xxPtry memory address 0x7f96c0x7f9600x7f95c value 50x7f96c? 5 5

Visualization of Pointers 10 int x = 5; int *xPtr = &x; /* xPtr points to x */ int y = *xPtr; /* y’s value is 5 */ x = 3; /* y is still 5 */ y = 2; /* x is still 3 */ variable name xxPtry memory address 0x7f96c0x7f9600x7f95c value 50x7f96c5 23

Visualization of Pointers 11 int x = 5; int *xPtr = &x; /* xPtr points to x */ int y = *xPtr; /* y’s value is 5 */ x = 3; /* y is still 5 */ y = 2; /* x is still 3 */ variable name xxPtry memory address 0x7f96c0x7f9600x7f95c value 30x7f96c2

Pointers and Arrays and Functions

Arrays and Functions Because arrays are pointers, they are always passed by address to a function What does this mean? – Program does not make a copy of an array – Any changes made to an array inside a function will remain after the function exits 13

Array Elements vs Arrays Passing one element of an array is still treated as pass by value For example – classNums[0] is a single variable of type int, and is passed to the function by value – classNums is an array, and is passed to the function by its address 14

C-Strings and Functions Reminder! C-style strings are arrays of characters So functions always pass C-Strings by… – Address! Pass to a function by name only – Just like any other array 15

C-Strings as Arguments In a function prototype, that looks like this: /* function takes a char pointer */ void toUpper (char *word); char str[] = "hello"; toUpper (str); This is also a valid function prototype: void toUpper (char word[]); 16

Passing Variables: 3 Options

Review: Passing by Value The “default” way to pass variables to functions // function prototype void printVal (int x); int x = 5; int *xPtr = &x; printVal(x); // function call printVal(*xPtr); // also valid call 18

Review: Passing by Address Uses pointers, and uses * and & operators // function prototype void changeVal (int *x); int x = 5; int *xPtr = &x; changeVal(&x); // function call changeVal(xPtr); // also valid call 19

Third Option: References References are – Safer than pointers – Less powerful – More restricted in usage Use the ampersand ( & ) for declaration int &xRef = x; 20

References Once created, references don’t need to use the ampersand or asterisk – They look like “normal” variables – But behave (somewhat) like pointers References must be initialized at declaration References cannot be changed References can be treated as another “name” for a variable (no dereferencing) 21

Functions and References Functions that take in references (instead of addresses) look almost identical to functions that take in “normal” values void changeByRef (int &x){ x = x + 1; } Prototype changes, but function body looks like that of a function that takes in a value 22

Calling Reference Functions Calling also looks similar to functions “by value” void changeByRef(int &x); //prototype int x = 5; int &xRef = x; //create reference changeByRef(x); //function call changeByRef(xRef); //also valid call 23

Downsides to References References are static – Once initialized, they are forever tied to the thing that they reference Using them looks identical to using a value – That’s a good thing though? It’s easier! – But it can also be confusing May think you’re passing by value, and that the contents of the variable won’t be changed 24

LIVECODING!!!

Announcements Project 1 has been released Found on Professor’s Marron website Due by 9:00 PM on February 23rd Get started on it now! Next time: Classes and Objects 26

Practice Problem Write a function called makeChange() that takes in a value in cents, represented as an int and then calculates the number of quarters, dimes, nickels, & pennies needed for change The function can take in multiple arguments The function does not return anything The cents value is guaranteed to be correct – A valid integer, positive, etc. 27