Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.

Slides:



Advertisements
Similar presentations
1 Pointers and Strings Section 5.4, , Lecture 12.
Advertisements

EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 (Pointers) Lecture 1 –Follow class notes –Some.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2007 Pearson Education, Inc. All rights reserved C Pointers.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
 2007 Pearson Education, Inc. All rights reserved. 1 C Pointers Chapter 7 from “C How to Program" Another ref:
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Dale Roberts, Lecturer Computer Science, IUPUI.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 7 - Pointers Outline 7.1 Introduction
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers and Pointer-Based Strings
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Scope, Parameter Passing, Storage Specifiers
Functions Declarations CSCI 230
Multidimensional Arrays
Pointers  Week 10.
Dale Roberts, Lecturer IUPUI
Systems Programming Concepts
Arrays Strings and Parameter Passing CSCI N305
Pointers Call-by-Reference CSCI 230
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Characters and Strings Functions
Dale Roberts, Lecturer IUPUI
CISC181 Introduction to Computer Science Dr
C Pointers Systems Programming.
Classes Member Qualifiers
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference

Dale Roberts Calling Functions by Reference Call by reference with pointer arguments Pass address of argument using & operator Allows you to change actual location in memory Arrays are not passed with & because the array name is already a pointer * Operator * Operator Used as formal parameter for variable inside of function void double_num ( int *number ) { *number = 2 * ( *number ); } *number used as nickname for the actual variable passed

Dale Roberts 1 /* 2 Cube a variable using call-by-value 3 */ 4 5#include 6 7int cubeByValue( int ); /* prototype */ 8 9int main() 10{ 11 int number = 5; printf( "The original value of number is %d", number ); 14 number = cubeByValue( number ); 15 printf( "\nThe new value of number is %d\n", number ); return 0; 18} 19 20int cubeByValue( int n ) 21{ 22 return n * n * n; /* cube number in main */ 23} The original value of number is 5 The new value of number is 125 Function prototype Initialize variables Call function Define function Program Output Example of Calling Functions by Value

Dale Roberts int main() { int number = 5; number = cubeByValue( number ); } int main() { int number = 5; number = cubeByValue( number ); } int main() { int number = 5; number = cubeByValue( number ); } int main() { int number = 5; number = cubeByValue( number ); } int main() { int number = 5; number = cubeByValue( number ); } int cubeByValue( int n ) { return n * n * n; } int cubeByValue( int n ) { return n * n * n; } int cubeByValue( int n ) { return n * n * n; } int cubeByValue( int n ) { return n * n * n; } int cubeByValue( int n ) { return n * n * n; } number n undefined n 5 n 5 n n 125 Example of Calling Functions by Value

Dale Roberts 1/* Fig. 7.7: fig07_07.c 2 Cube a variable using call-by-reference 3 with a pointer argument */ 4 5#include 6 7void cubeByReference( int * ); /* prototype */ 8 9int main() 10{ 11 int number = 5; printf( "The original value of number is %d", number ); 14 cubeByReference( &number ); 15 printf( "\nThe new value of number is %d\n", number ); return 0; 18} 19 20void cubeByReference( int *nPtr ) 21{ 22 *nPtr = *nPtr * *nPtr * *nPtr; /* cube number in main */ 23} The original value of number is 5 The new value of number is 125 Notice how the address of number is given - cubeByReference expects a pointer (an address of a variable). Inside cubeByReference, *nPtr is used (* nPtr is number ). Notice that the function prototype takes a pointer to an integer ( int * ). Function prototype Initialize variables Call function Define function Program Output Example of Calling Functions by Reference

Dale Roberts Example of Calling Functions by Reference int main() { int number = 5; cubeByReference( &number ); } int main() { int number = 5; cubeByReference( &number ); } int main() { int number = 5; cubeByReference( &number ); } void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; } void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; } void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; } number 5 nPtr undefined n nPtr Before the call by reference to cubeByReference: After call by reference to cubeByReference and before *nPtr is cubed: After *nPtr is cubed : number 5 nPtr address of number 125 address of number

Dale Roberts COMPUTER MEMORY CONSTANT MEMORY AREA VARIABLE MEMORY AREA x1 myPtr1 x2 myPtr2 x3 Ptr x myPtr case 1 case 2 case 3 case with using const Using the const Qualifier with Pointers const qualifier Variable cannot be changed Use const if function does not need to change a variable Attempting to change a const variable produces an error const pointers Point to a constant memory location Must be initialized when declared 1) int *const myPtr1 = &x1; Type int *const Constant pointer to an int x can be changed, but not *Ptr 2) const int *myPtr2 = &x2; Regular pointer to a const int 3) const int *const Ptr = &x3; const pointer to a const int

Dale Roberts 1/* Fig. 7.13: fig07_13.c 2 Attempting to modify a constant pointer to 3 non-constant data */ 4 5#include 6 7int main() 8{8{ 9 int x, y; int * const ptr = &x; /* ptr is a constant pointer to an 12 integer. An integer can be modified 13 through ptr, but ptr always points 14 to the same memory location. */ 15 *ptr = 7; 16 ptr = &y; return 0; 19} FIG07_13.c: Error E2024 FIG07_13.c 16: Cannot modify a const object in function main *** 1 errors in Compile *** Changing *ptr is allowed – x is not a constant. Changing ptr is an error – ptr is a constant pointer. Declare variables Declare const pointer to an int Change *ptr (which is x ) Attempt to change ptr Output