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

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
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.
 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.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 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:
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 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.
Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on.
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.
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.
© 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 Object-Oriented Programming Using C++ A tutorial for pointers.
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 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer.
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.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
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.
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
Chapter 7 from “C How to Program"
Remark: Data Type of Array Name
Pointers and Pointer-Based Strings
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
7 C Pointers.
EENG212 ALGORITHMS & DATA STRUCTURES
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:

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

Dale Roberts Pointer Expressions and Pointer Arithmetic pv+n  pv + n*sizeof ( variable type that pointer point to ) Arithmetic operations can be performed on pointers Increment/decrement pointer ( ++ or -- ) Example: ++vPtr, vPtr++, --vPtr, vPtr-- Add an integer to a pointer( + or +=, - or -= ) Pointers may be subtracted from each other Operations meaningless unless performed on an array

Dale Roberts Example: Five element int array on machine with 4 byte int s vPtr points to first element v[ 0 ] whose address location is 3000 ( vPtr = 3000 ) vPtr points to first element v[ 0 ] whose address location is 3000 ( vPtr = 3000 ) vPtr += 2 ; vPtr += 2 ; // sets vPtr to 3008 vPtr points to v[ 2 ] (incremented by 2), but the machine has 4 byte integers, so it points to address 3008 vPtr points to v[ 2 ] (incremented by 2), but the machine has 4 byte integers, so it points to address 3008 pointer variable vPtr v[0]v[1]v[2]v[4]v[3] location

Dale Roberts Example: (double pointer) Assume long (long integer) is 4 bytes, and pointer variable is 2 bytes. long a[10]={5, 10, 15, …}; long *pa, **ppa; int i=5; pa = &a; ppa = &pa; Pointer Expressions and Pointer Arithmetic VariableAddressValue a …… pa ppa ExpressionValueNote pa *4 pa *4 pa+i i*4 ppa *2 ppa+i i*2 *pa *(pa+1)10 a[1]=pa[1]=*(a+1) pa[2]15648 *ppa640value of pa *ppa+1644pa+1 *(ppa+1)invalid*(702) **ppa+16a[0]+1 = 5+1 *(*ppa+1)10 *(pa+1)=*(640+1*4) ppapa a Questions:

Dale Roberts Pointer Expressions and Pointer Arithmetic Subtracting pointers Returns number of elements from one to the other. If vPtr2 is a pointer pointing to v[ 2 ]; vPtr is a pointer pointing to v[ 0 ]; vPtr2 - vPtr would produce 2 vPtr2 - vPtr would produce 2 Pointer comparison ( ) See which pointer points to the higher numbered array element Also, see if a pointer points to 0 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

Dale Roberts The Relationship Between Pointers and Arrays Arrays and pointers are closely related Array name like a constant pointer Pointers can do array subscripting operations Example: Declare an array b[ 5 ] and a pointer bPtr bPtr = b; // To set them equal to one another // The array name ( b ) is actually the address of first element of the array bPtr = &b[ 0 ]; // Explicitly assigns bPtr to address of first element of b To access element b[ 3 ]: x=*( bPtr + 3 ) // Where n is the offset. Called pointer/offset notation x=bptr[ 3 ] // Called pointer/subscript notation // bPtr[ 3 ] same as b[ 3 ] x=*( b + 3 ) // Performing pointer arithmetic on the array itself

Dale Roberts Pointers and Arrays Strong relation between pointers and arrays Pointers and arrays can be used interchangeably. The array name is equivalent to the address of the first element in the array Example: int a[10]; int *pa; pa = &a[0]; /* is equivalent to pa = a */ So, a[1]  *(pa+1)  pa[1]  *(a+1) &a[1]  pa+1  a+1 a[i]  *(pa+i)  pa[i]  *(a+i) &a[i]  pa+i  a+i a[i]+=5  *(pa+i)+=5  pa[i]+=5 Example: f(int s[]) { … } f(int *s) { … } 

Dale Roberts Arrays of Pointers Arrays can contain pointers For example: an array of strings char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"}; Strings are pointers to the first character char * – each element of suit is a pointer to a char char * – each element of suit is a pointer to a char The strings are not actually stored in the array suit, only pointers to the strings are stored suit array has a fixed size, but strings can be of any size suit array has a fixed size, but strings can be of any size suit[3] suit[2] suit[1] suit[0]’H’’e’’a’’r’’t’’s’ ’\0’ ’D’’i’’a’’m’’o’’n’’d’’s’ ’\0’ ’C’’l’’u’’b’’s’ ’\0’ ’S’’p’’a’’d’’e’’s’ ’\0’

Dale Roberts Pointers 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

Dale Roberts void swap( int *element1Ptr, int *element2Ptr ) { int temp; temp = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = temp; } int ascending( int a, int b ) { return b < a; /*swap if b is less than a*/ } int descending( int a, int b ) { return b > a; /*swap if b is greater than a*/ } Example: Bubble Sort #define SIZE 10 void bubble(int [],const int, int (*)(int,int)); int ascending( int, int ); int descending( int, int ); int main() { int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; bubble( a, SIZE, ascending ); bubble( a, SIZE, descending ); } void bubble( int work[], const int size, int (*compare)(int,int)) { 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])) swap(&work[count],&work[count + 1]); }

Dale Roberts Function bubble takes a function pointer bubble calls this helper function 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 ints and returns a bool tells bubblesort to expect a pointer to a function that takes two ints 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