1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.

Slides:



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

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Object-Oriented Programming in C++
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Workin’ with Pointas An exercise in destroying your computer.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Review 1 List Data Structure List operations List Implementation Array Linked List.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Pointers and Pointer-Based Strings
Student Book An Introduction
Pointer.
Pointer Basics Psst… over there.
Chapter 9: Pointers.
Object Oriented Programming COP3330 / CGS5409
Alternate Version of STARTING OUT WITH C++ 4th Edition
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Functions Pass By Value Pass by Reference
Dynamic Memory A whole heap of fun….
Simulating Reference Parameters in C
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
Pointers and dynamic objects
The Stack.
Pointer Basics Psst… over there.
Pointers, Dynamic Data, and Reference Types
C Parameter Passing.
Presentation transcript:

1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int x = 3, y = 5; swap(x,y); return 0; } void swap(int *a, int *b) { int temp =* a; *a = *b; *b = temp; } int main () { int x = 3, y = 5; swap(&x, &y); return 0; }

2 Pointers & functions Function arguments that are pointers copy an address to the formal parameters Dereferencing the formal parameter lets us modify the value pointed to by the pointer. But changing the pointer itself NEVER changes the actual parameter Example 1: int main () { int x, *px; px = &x; initialize(px); return 0; } void initialize (int *ptr) { *ptr = 34; } here x has become 34

3 Pointers & functions Function arguments that are pointers copy an address to the formal parameters Dereferencing the formal parameter lets us modify the value pointed to by the pointer. But changing the pointer itself NEVER changes the actual parameter Example 2: int main () { int x, *px; px = &x; initialize(px); return 0; } void initialize (int *ptr) { *ptr = 34; ptr++; } here x has become 34 but px is unchanged. ptr goes out of scope when we exit the function

4 Pointers & functions Be very careful with functions that return pointers. Make certain that the pointer points to something valid on return and that space has been properly allocated. Example: int * func () { int x, *p; x = 10; p = &x; return p; } int main () { int *ptr ; ptr = func(); cout << *ptr << endl; return 0; } This program will print out garbage. Why? p and x are local variables, statically stored in func()'s stack frame. They "disappear" on exit from func(), so *ptr cannot contain the value of x.

5 More on pass by reference When an argument is passed by value, enough memory must be allocated for a copy of that argument. This may slow down our program if the argument is a large object. The solution is to pass it by reference. HOWEVER, we may not want to allow the function to modify the value of the argument. In that case, we must use the keyword const to specify that the argument cannot be changed. This is called a const reference

6 Examples Example 1: class bigTypeT {... }; void func (const bigTypeT &arg) { // arg cannot be modified in here } int main () { bigTypeT arg; func(arg); return 0; }

7 Examples Example 2: void func (const int arr[ ]) { // arr cannot be modified here } int main () { int arr[10]; func(arr); return 0; }

8 Examples Example 3: void func (const int *ptr) { // *ptr cannot be modified // ptr can be modified but this will have no effect in main() // since ptr is local to func() } int main () { int *pnum, x; pnum = &x; func(pnum); return 0; }

9 Examples Example 4: class Ship {... } int main () { Ship *fleet[10]; // an array of pointers to Ship objects for (int i=0; i<10; i++) { fleet[i] = new Ship; } return 0; }

10 Examples Example 5: typedef enum {FRIGATE, DESTROYER, CRUISER} USshipT; class Ship { public: USshipT type;... } int main () { Ship *boat; // an array of pointers to Ship objects boat = new Ship; (*boat).type = FRIGATE; boat -> type = FRIGATE; return 0; } these statements are exactly the same. The -> operator is shorthand for *.