DOC PREVIEW
IUPUI CSCI 23000 - Pointer Arithmetic

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Pointer Expressions and Pointer ArithmeticSlide 3Slide 4Slide 5The Relationship Between Pointers and ArraysPointers and ArraysArrays of PointersPointers to FunctionsExample: Bubble SortSlide 11Dale RobertsDepartment of Computer and Information Science,Department of Computer and Information Science,School of Science, IUPUISchool of Science, IUPUICSCI 230PointersPointer ArithmeticPointer ArithmeticDale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected] RobertsPointer Expressions and Pointer ArithmeticPointer Expressions and Pointer Arithmeticpv+n pv + n*sizeof(variable type that pointer point to)Arithmetic operations can be performed on pointersArithmetic operations can be performed on pointersIncrement/decrement pointer (Increment/decrement pointer (++++ or or ----))ExampleExample: : ++vPtr, vPtr++,++vPtr, vPtr++,--vPtr, vPtr----vPtr, vPtr--Add an integer to a pointer( Add an integer to a pointer( ++ or or +=+= , , -- or or -=-=))Pointers may be subtracted from each otherPointers may be subtracted from each otherOperations meaningless unless performed on an arrayOperations meaningless unless performed on an arrayDale RobertsExample:Five element Five element intint array on machine with 4 byte array on machine with 4 byte intintss vPtrvPtr points to first element points to first element v[v[ 00 ]]whose address whose address location is location is 30003000 ( (vPtr = 3000vPtr = 3000)) vPtr += 2vPtr += 2;;// // sets sets vPtrvPtr to to 30083008 vPtrvPtr points to points to v[v[ 22 ]] (incremented by 2), but (incremented by 2), but the machine has 4 byte the machine has 4 byte integersintegers, so it points to , so it points to address address 30083008pointer variable vPtrv[0] v[1] v[2] v[4]v[3]3000 3004 3008 3012 3016locationDale RobertsExample: (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 ArithmeticPointer Expressions and Pointer ArithmeticVariable Address Value a640 5644 10648 15… …pa 700 640ppa 800 700Expression Value Notepa+1 644 640+1*4pa+3 652 640+3*4pa+i 660 640+i*4ppa+1 702 700+1*2ppa+i 710 700+i*2*pa+1 6 5+1*(pa+1) 10a[1]=pa[1]=*(a+1)pa[2] 15 648*ppa 640 value of pa*ppa+1 644 pa+1*(ppa+1) invalid *(702)**ppa+1 6 a[0]+1 = 5+1*(*ppa+1) 10*(pa+1)=*(640+1*4)ppa pa640700700800a51015640644648652656660Questions:Dale RobertsPointer Expressions and Pointer ArithmeticPointer Expressions and Pointer ArithmeticSubtracting pointersSubtracting pointersReturns number of elements from one to the other. IfReturns number of elements from one to the other. IfvPtr2 is a pointer pointing to v[vPtr2 is a pointer pointing to v[ 22 ];];vPtr is a pointer pointing to v[vPtr is a pointer pointing to v[ 00 ];]; vPtr2 - vPtrvPtr2 - vPtr would produce 2 would produce 2Pointer comparison ( Pointer comparison ( <<, , ==== , , >> ) )See which pointer points to the higher numbered array elementSee which pointer points to the higher numbered array elementAlso, see if a pointer points to Also, see if a pointer points to 00Pointers of the same type can be assigned to each otherPointers of the same type can be assigned to each otherIf not the same type, a cast operator must be usedIf not the same type, a cast operator must be usedException: pointer to Exception: pointer to voidvoid (type (type void *void *))Generic pointer, represents any typeGeneric pointer, represents any typeNo casting needed to convert a pointer to No casting needed to convert a pointer to voidvoid pointer pointervoidvoid pointers cannot be dereferenced pointers cannot be dereferencedDale RobertsThe Relationship Between Pointers and ArraysThe Relationship Between Pointers and ArraysArrays and pointers are closely relatedArrays and pointers are closely relatedArray name like a constant pointerArray name like a constant pointerPointers can do array subscripting operationsPointers can do array subscripting operationsExample: Declare an array Example: Declare an array b[b[ 55 ]] and a pointer and a pointer bPtrbPtrbPtr = b;bPtr = b;// // To set them equal to one anotherTo set them equal to one another//// The array name (The array name (bb) is actually the address of first element of the array) is actually the address of first element of the arraybPtr = &b[bPtr = &b[ 00 ];];// // Explicitly assigns Explicitly assigns bPtrbPtr to address of first element of to address of first element of bbTo access element To access element b[b[ 33 ]:]: x=*(x=*( bPtrbPtr ++ 33 ))// // Where Where nn is the offset. Called pointer/offset notation is the offset. Called pointer/offset notationx=bptr[x=bptr[ 33 ]] // // Called pointer/subscript notationCalled pointer/subscript notation// bPtr[// bPtr[ 33 ]] same as same as b[b[ 33 ]]x=*(x=*( bb ++ 33 )) // // Performing pointer arithmetic on the array itselfPerforming pointer arithmetic on the array itselfDale RobertsPointers and ArraysPointers and ArraysStrong relation between pointers and arraysStrong relation between pointers and arraysPointers and arrays can be used interchangeably.Pointers and arrays can be used interchangeably.The array name is equivalent to the address of the first The array name is equivalent to the address of the first element in the arrayelement in the arrayExampleExample::int a[10];int a[10];int *pa;int *pa;pa = &a[0]; /* is equivalent to pa = a */pa = &a[0]; /* is equivalent to pa = a */So, So, a[1]a[1] *(pa+1) *(pa+1) pa[1] pa[1]  *(a+1) *(a+1)&a[1]&a[1] pa+1 pa+1 a+1 a+1a[i]a[i] *(pa+i) *(pa+i) pa[i] pa[i]  *(a+i) *(a+i)&a[i]&a[i] pa+i pa+i a+i a+ia[i]+=5a[i]+=5 *(pa+i)+=5 *(pa+i)+=5  pa[i]+=5 pa[i]+=5ExampleExample::f(int f(int s[]s[])){ … { … }}f(int *s){ … }Dale RobertsArrays of PointersArrays of PointersArrays can contain pointersArrays can contain pointersFor example: an array of stringsFor example: an array of stringschar *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};Strings are pointers to the first characterStrings are pointers to the first character char *char * –– each element of each element of suitsuit is a pointer to a is a pointer to a charcharThe strings are not actually stored in the array The strings are not actually stored in the array suitsuit, only pointers to , only pointers to the strings are storedthe strings are


View Full Document

IUPUI CSCI 23000 - Pointer Arithmetic

Download Pointer Arithmetic
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Pointer Arithmetic and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Pointer Arithmetic 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?