H-SC COMS 262 - Lecture 2 - Pointers 2

Unformatted text preview:

IntroductionPointer ArithmeticPointer + intPointer - PointerPointers and ArraysPointers as Function ParametersAssignmentPointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointersLecture 2Section 2.4Robb T. KoetherHampden-Sydney CollegeFri, Jan 16, 2009PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentOutline1Introduction2Pointer ArithmeticPointer + intPointer - Pointer3Pointers and Arrays4Pointers as Function Parameters5AssignmentPointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentBig Endian and Little EndianSuppose we make the assignmentint i = 0x25116634;If the architecture is Big Endian, then it is stored as25 346611"Big" end firstIf the architecture is Little Endian, then it is stored as34 251166"Little" end firstPointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentBig Endian and Little EndianDefinition (Big Endian, Little Endian)If a processor has Big Endian architecture, then it stores thehighest-order byte of the value in the lowest byte address.If a processor has Little Endian architecture, then it storesthe lowest-order byte of the value in the lowest byte address.Multi-byte objects are addressed by their lowest byteaddress.PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer + intIt is permissible to add an integer to a pointer.int i = 123;int*p = &i;int*q = p + 1;The integer is multiplied by the size of the objectpointed to.That value is added to the address.The result is a pointer of the same type.PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer + intThe integer is interpreted as the number of objects ofthat type between the original address and the newaddress.The resulting address is interpreted as a pointer to theobject located the specified number of objects to theright, as in an array.PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer Arithmeticint i = 123; // Addr of i is 0x0100int*p = &i; // p = 0x0100char*q = (char*)&i; // q = 0x0100short*r = (short*)&i; // r = 0x01000x0100(i)PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer Arithmeticint i = 123; // Addr of i is 0x0100int*p = &i; // p = 0x0100char*q = (char*)&i; // q = 0x0100short*r = (short*)&i; // r = 0x01000x0100(i)p p + 1 p + 2p - 10x0104 0x01080x00fcPointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer Arithmeticint i = 123; // Addr of i is 0x0100int*p = &i; // p = 0x0100char*q = (char*)&i; // q = 0x0100short*r = (short*)&i; // r = 0x01000x0100(i)q q + 1 q + 2q - 10x0104 0x01080x00fcPointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer Arithmeticint i = 123; // Addr of i is 0x0100int*p = &i; // p = 0x0100char*q = (char*)&i; // q = 0x0100short*r = (short*)&i; // r = 0x01000x0100(i)r r + 1 r + 2r - 10x0104 0x01080x00fcPointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentDemoDemo - PtrPlusPtr.cppRun PtrPlusPtr.cpp.PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentDemoDemo - Endianness.cppRun Endianness.cpp.PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer SubtractionSubtraction of one pointer from another pointer (of thesame type) is permitted.Take the difference of the addresses.Divide it by the size of the object pointed to.The result is an int.Interpret the result as the number of objects of that typebetween two addresses.PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer SubtractionExample (Pointer Subtraction)int*p;int*q;int a = q - p;p qPointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer SubtractionExample (Pointer Subtraction)int*p;int*q;int a = q - p;p qq - p = 5PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentDemoDemo - PtrMinusPtr.cppRun PtrMinusPtr.cpp.PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer ArithmeticExampleint a = (p2 - p1) + 5;int b = p2 - (p1 - 5);int c = (p2 + 5) - p1;These are legal operations.Are they equivalent?PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointer ArithmeticExample (Illegal Addition)int*mid = (p2 + p1)/2; // Midpt b/t p1, p2Pointer addition is illegal.How can we obtain a pointer to the object halfway betweenp1 and p2?PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentPointers and ConstantsExample (Pointers and Constants)int const i = 123;int j = 456;int const*pci = &i;int*const cpi = &j;int const*const cpci = &i;A pointer may point to a constant - the object pointed tocannot be changed.A pointer itself may be constant - the pointer cannot bechanged.In fact, a constant pointer may point to a constantobject!PointersRobb T.KoetherIntroductionPointerArithmeticPointer + intPointer - PointerPointers andArraysPointers asFunctionParametersAssignmentAn Array Name as a PointerExample (Pointers and Arrays)int a[3] = {10, 20, 30};int*pi = a;cout <<*pi << endl;cout <<*(pi + 1) << endl;cout <<*(pi + 2) << endl;An array name represents a pointer that points to thefirst member of the array.The array name is a constant pointer; its value cannotbe changed.An array name may be


View Full Document

H-SC COMS 262 - Lecture 2 - Pointers 2

Download Lecture 2 - Pointers 2
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 Lecture 2 - Pointers 2 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 Lecture 2 - Pointers 2 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?