110 likes | 293 Views
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. Pointer Expressions and Pointer Arithmetic. pv+n
E N D
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
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
location 3000 3004 3008 3012 3016 pointer variablevPtr v[0] v[1] v[2] v[4] v[3] Example: • Five element int array on machine with 4 byte ints • vPtr points to first element v[0]whose address location is 3000 (vPtr = 3000) • vPtr += 2; // sets vPtrto 3008 • vPtr points to v[2] (incremented by 2), but the machine has 4 byte integers, so it points to address 3008
a 5 640 644 10 800 700 648 15 700 640 652 ppa pa 656 660 Pointer Expressions and Pointer Arithmetic 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; Questions:
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 • 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
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
f(int *s) { … } 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[]) { … }
’\0’ ’\0’ ’\0’ ’\0’ ’o’ ’d’ ’n’ ’u’ ’a’ ’s’ ’b’ ’d’ ’a’ ’s’ ’s’ ’a’ ’i’ ’m’ ’e’ ’r’ ’l’ ’e’ ’s’ ’t’ ’p’ suit[0] ’H’ ’D’ suit[1] ’C’ suit[2] ’S’ suit[3] 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 • The strings are not actually stored in the array suit, only pointers to the strings are stored • suitarray has a fixed size, but strings can be of any size
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
Example: Bubble Sort #define SIZE 10 void bubble(int [],constint, 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[], constint 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]); } voidswap( int *element1Ptr, int *element2Ptr ) { int temp; temp = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = temp; } intascending( int a, int b ) { return b < a; /*swap if b is less than a*/ } intdescending( int a, int b ) { return b > a; /*swap if b is greater than a*/ }
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 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