Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer."— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

2 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

3 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] 30003004300830123016 location

4 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 a6405 64410 64815 …… pa700640 ppa800700 ExpressionValueNote pa+1644640+1*4 pa+3652640+3*4 pa+i660640+i*4 ppa+1702700+1*2 ppa+i710700+i*2 *pa+165+1 *(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 640700 800 a 5 10 15 640 644 648 652 656 660 Questions:

5 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

6 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

7 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) { … } 

8 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’

9 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

10 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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3592352/slides/slide_10.jpg", "name": "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 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

11 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


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

Similar presentations


Ads by Google