EPSII 59:006 Spring 2004.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
CPT: Pointers/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (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.
5.1Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings.
Pointers Ethan Cerami Fundamentals of Computer New York University.
 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.
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.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 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.
 2007 Pearson Education, Inc. All rights reserved. 1 C Pointers Chapter 7 from “C How to Program" Another ref:
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
 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 Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Dale Roberts, Lecturer Computer Science, IUPUI.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
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.
Lecture 17: The Last Puzzle Piece with Functions.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
1 CSC 211 Data Structures Lecture 5 Dr. Iftikhar Azim Niaz 1.
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
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Week 9 - Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
POINTERS.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 7 from “C How to Program"
Pointers  Week 10.
Systems Programming Concepts
Pointers and Pointer-Based Strings
Pointers Call-by-Reference CSCI 230
Pointers Kingdom of Saudi Arabia
Chapter 5 - Pointers and Strings
5.1 Introduction Pointers Powerful, but difficult to master
7 C Pointers.
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
5.1 Introduction Pointers Powerful, but difficult to master
POINTERS.
C++ Programming Lecture 18 Pointers – Part II
CISC181 Introduction to Computer Science Dr
C Pointers Systems Programming.
C Pointers Another ref:
Presentation transcript:

EPSII 59:006 Spring 2004

Announcements No TA Office Hours today (March 3) after 4:30 p.m. Next homework assignment: Will be posted on Friday, March 5 Due on Friday, March 12

Pointers Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Bubble Sort Using Call by Reference

Introduction Pointers Powerful, but difficult to master concept Used for call-by-reference parameter passage Close relationship with arrays and strings

Pointer Variable Declarations and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain address of a variable that has a specific value (indirect reference) Indirection – referencing a pointer value   count 7 count 7 countPtr

Pointer Variable Declarations and Initialization Pointer declarations * used with pointer variables int *myPtr; Declares a pointer to an int (pointer of type int *) Multiple pointers require using a * before each variable declaration int *myPtr1, *myPtr2; Can declare pointers to any data type Initialize pointers to 0, NULL, or an address 0 or NULL – points to nothing (NULL preferred)

Pointer Operators & (address operator) References the address of a variable int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y // yPtr “points to” y yPtr y 5 yptr 500000 600000 Address of y is value of yptr

Pointer Operators * (indirection/dereferencing operator) References the variable “pointed to” by a pointer variable *yPtr refers to y (because yPtr points to y) * can be used for assignment: *yPtr = 7; // changes y to 7 Dereferenced pointer (operand of *) must be an lvalue (no constants) * and & are inverses They cancel each other out That is: *&x == x

Pointers—A simple example int main(void) { float x, y; float *p, *q; // p is of type “pointer to float” p = &x; // assigns address of x to p -- i.e. p “points to” x q = &y; // assigns address of y to q – i.e. q “points to” y /* Now the following two statements are equivalent */ x = 7.0; *p = 7.0; /* and the following three statements are equivalent */ y = x; y = *p; *q = *p; } Notes: Read &x as “the address of variable x” Read *p as “the memory location pointed to by p” — i.e. x

Declare variables Initialize variables Print Program Output 1 /* Fig. 7.4: fig07_04.c 2 Using the & and * operators */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int a; /* a is an integer */ 8 int *aPtr; /* aPtr is a pointer to an integer */ 9 10 a = 7; 11 aPtr = &a; /* aPtr set to address of a */ 12 13 printf( "The address of a is %p" 14 "\nThe value of aPtr is %p", &a, aPtr ); 15 16 printf( "\n\nThe value of a is %d" 17 "\nThe value of *aPtr is %d", a, *aPtr ); 18 19 printf( "\n\nShowing that * and & are complements of " 20 "each other.\n&*aPtr = %p" 21 "\n*&aPtr = %p\n", &*aPtr, *&aPtr ); 22 23 return 0; 24 } Declare variables Initialize variables Print Program Output The address of a is the value of aPtr. The * operator returns an alias to what its operand points to. aPtr points to a, so *aPtr returns a. Notice how * and & are complements The address of a is 0012FF88 The value of aPtr is 0012FF88 The value of a is 7 The value of *aPtr is 7 Showing that * and & are complements of each other. &*aPtr = 0012FF88 *&aPtr = 0012FF88

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 Used as alias/nickname for variable inside of function void double( int *number ) { *number = 2 * ( *number ); } *number used as nickname for the variable passed

Call-by Reference– A simple example A function that swaps the value of two variables First, a version that DOESN’T work: void swap( int , int); int main(void) { int x = 3; int y = 5; printf(“x=%d y=%d\n”, x,y); swap(x, y); } void swap(int a, int b) { int temp; temp = a; a = b; b = temp; swap has no effect on the values of x and y due to pass-by-value parameter passage x=3 y=5

Call-by-reference– a simple example Now, a version of swap that works: void swap( int *, int *); int main(void) { int x = 3; int y = 5; printf(“x=%d y=%d\n”, x,y); swap(&x, &y); } void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; x=3 y=5 x=5 y=3

Function prototype Initialize variables Call function Define function 1 /* Fig. 7.7: fig07_07.c 2 Cube a variable using call-by-reference 3 with a pointer argument */ 4 5 #include <stdio.h> 6 7 void cubeByReference( int * ); /* prototype */ 8 9 int main() 10 { 11 int number = 5; 12 13 printf( "The original value of number is %d", number ); 14 cubeByReference( &number ); 15 printf( "\nThe new value of number is %d\n", number ); 16 17 return 0; 18 } 19 20 void cubeByReference( int *nPtr ) 21 { 22 *nPtr = *nPtr * *nPtr * *nPtr; /* cube number in main */ 23 } Notice that the function prototype takes a pointer to an integer (int *). Function prototype Initialize variables Call function Define function Program Output 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). The original value of number is 5 The new value of number is 125

Bubble Sort Using Call-by-reference Implement bubblesort using pointers Swap two elements swap function must receive address (using &) of array elements Array elements have call-by-value default Using pointers and the * operator, swap can switch array elements Psuedocode Initialize array print data in original order Call function bubblesort print sorted array Define bubblesort

Initialize array Declare variables Print array Call bubbleSort Bubblesort is passed the address of array a. The name of an array is a pointer, so & is not needed.

Function definitions Program Output Could also have specified the parameter as: int array[ ] 33 int pass, j; 34 for ( pass = 0; pass < size - 1; pass++ ) 35 36 for ( j = 0; j < size - 1; j++ ) 37 38 if ( array[ j ] > array[ j + 1 ] ) 39 swap( &array[ j ], &array[ j + 1 ] ); 40 } 41 42 void swap( int *element1Ptr, int *element2Ptr ) 43 { 44 int hold = *element1Ptr; 45 *element1Ptr = *element2Ptr; 46 *element2Ptr = hold; 47 } Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45