1 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer.

Slides:



Advertisements
Similar presentations
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
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.
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.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 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.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Esempi Puntatori e Stringhe1 // This program puts values into an array, sorts the values into // ascending order, and prints the resulting array. #include.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
(continue) © by Pearson Education, Inc. All Rights Reserved.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
 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 Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Dale Roberts, Lecturer Computer Science, IUPUI.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
© 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, ,
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
PGT 106 C Programming POINTERS. PGT 106 C Programming Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling 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
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Computer Skills2 for Scientific Colleges
Week 9 - Pointers.
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 7 from “C How to Program"
Remark: Data Type of Array Name
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Pointers and Pointer-Based Strings
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
Chapter 5 - Pointers and Strings
7 C Pointers.
EENG212 ALGORITHMS & DATA STRUCTURES
5.1 Introduction Pointers Powerful, but difficult to master
POINTERS.
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 18 Pointers – Part II
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

1 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer to a pointer( + or +=, - or -= ) –Pointers may be subtracted from each other –Operations meaningless unless performed on an array

2 7.7Pointer Expressions and Pointer Arithmetic (II) 5 element int array on machine with 4 byte int s –vPtr points to first element v[0] at location ( vPtr = 3000 ) –vPtr +=2; sets vPtr to 3008 vPtr points to v[2] (incremented by 2), but machine has 4 byte int s. pointer variable vPtr v[0]v[1]v[2]v[4]v[3] location

3 7.7Pointer Expressions and Pointer Arithmetic (III) Subtracting pointers –Returns number of elements from one to the other. vPtr2 = v[2]; vPtr = v[0]; vPtr2 - vPtr == 2. Pointer comparison ( ) –See which pointer points to the higher numbered array element –Also, see if a pointer points to 0

4 7.7Pointer Expressions and Pointer Arithmetic (IV) Pointers of the same type can be assigned to each other –If not the same type, a cast operator must be used –Exception: pointer to void (type void * ) Generic pointer, represents any type No casting needed to convert a pointer to void pointer void pointers cannot be dereferenced

5 7.8The Relationship Between Pointers and Arrays Arrays and pointers closely related –Array name like a constant pointer –Pointers can do array subscripting operations Declare an array b[5] and a pointer bPtr bPtr = b; Array name actually a address of first element OR bPtr = &b[0] Explicitly assign bPtr to address of first element

6 7.8The Relationship Between Pointers and Arrays (II) Element b[n] –can be accessed by *( bPtr + n ) –n - offset (pointer/offset notation) –Array itself can use pointer arithmetic. b[3] same as *(b + 3) –Pointers can be subscripted (pointer/subscript notation) bPtr[3] same as b[3]

7 7.11Pointers to Functions Pointer to function –Contains address of function –Similar to how array name is address of first element –Function name is starting address of code that defines function Function pointers can be –Passed to functions –Stored in arrays –Assigned to other function pointers

Pointers to Functions (II) Example: bubblesort –Function bubble takes a function pointer bubble calls this helper function this determines ascending or descending sorting –The argument in bubblesort for the function pointer: bool ( *compare )( int, int ) tells bubblesort to expect a pointer to a function that takes two int s and returns a bool. –If the parentheses were left out: bool *compare( int, int ) Declares a function that receives two integers and returns a pointer to a bool

Outline 9 1. Initialize array. 2. Prompt for ascending or descending sorting. 2.1 Put appropriate function pointer into bubblesort. 2.2 Call bubble. 3. Print results. 1/* Fig. 7.26: fig07_26.c 2 Multipurpose sorting program using function pointers */ 3#include 4#define SIZE 10 5void bubble( int [], const int, int (*)( int, int ) ); 6int ascending( int, int ); 7int descending( int, int ); 8 9int main() 10{ int order, 13 counter, 14 a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; printf( "Enter 1 to sort in ascending order,\n" 17 "Enter 2 to sort in descending order: " ); 18 scanf( "%d", &order ); 19 printf( "\nData items in original order\n" ); for ( counter = 0; counter < SIZE; counter++ ) 22 printf( "%5d", a[ counter ] ); if ( order == 1 ) { 25 bubble( a, SIZE, ascending ); 26 printf( "\nData items in ascending order\n" ); 27 } 28 else { 29 bubble( a, SIZE, descending ); 30 printf( "\nData items in descending order\n" ); 31 } 32 Notice the function pointer parameter.

Outline Define functions. 33 for ( counter = 0; counter < SIZE; counter++ ) 34 printf( "%5d", a[ counter ] ); printf( "\n" ); return 0; 39} 40 41void bubble( int work[], const int size, 42 int (*compare)( int, int ) ) 43{ 44 int pass, count; void swap( int *, int * ); for ( pass = 1; pass < size; pass++ ) for ( count = 0; count < size - 1; count++ ) if ( (*compare)( work[ count ], work[ count + 1 ] ) ) 53 swap( &work[ count ], &work[ count + 1 ] ); 54} 55 56void swap( int *element1Ptr, int *element2Ptr ) 57{ 58 int temp; temp = *element1Ptr; 61 *element1Ptr = *element2Ptr; 62 *element2Ptr = temp; 63} 64 ascending and descending return true or false. bubble calls swap if the function call returns true. Notice how function pointers are called using the dereferencing operator. The * is not required, but emphasizes that compare is a function pointer and not a function.

Outline Define functions. Program Output 65int ascending( int a, int b ) 66{ 67 return b < a; /* swap if b is less than a */ 68} 69 70int descending( int a, int b ) 71{ 72 return b > a; /* swap if b is greater than a */ 73} Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 1 Data items in original order Data items in ascending order Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 2 Data items in original order Data items in descending order

12 兩個整數的最大公因數 (GCD) 與最小公倍數 (LCD) ,請用 C 程式函數 (function) 寫出。 GCD(a, b) 為最大公因數函數, LCM(a, b) 為最 小公倍數函數。 假設 a, b 為兩整數 ( 由鍵盤輸入 ) , 當鍵盤輸入為 1 ,執行函數 當鍵盤輸入為 2 ,執行函數 當鍵盤輸入為 3 ,執行函數 與 當鍵盤輸入為 0 ,程式停止 Home work #10