Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
PointersLecture 2
Section 2.4
Robb T. Koether
Hampden-Sydney College
Fri, Jan 16, 2009
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Outline
1 Introduction
2 Pointer ArithmeticPointer + intPointer - Pointer
3 Pointers and Arrays
4 Pointers as Function Parameters
5 Assignment
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Big Endian and Little Endian
Suppose we make the assignmentint i = 0x25116634;
If the architecture is Big Endian, then it is stored as
25 346611"Big" end first
If the architecture is Little Endian, then it is stored as
34 251166"Little" end first
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Big Endian and Little Endian
Definition (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.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer + int
It 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.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer + int
The 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.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer Arithmetic
int i = 123; // Addr of i is 0x0100int* p = &i; // p = 0x0100char* q = (char*)&i; // q = 0x0100short* r = (short*)&i; // r = 0x0100
0x0100(i)
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer Arithmetic
int i = 123; // Addr of i is 0x0100int* p = &i; // p = 0x0100char* q = (char*)&i; // q = 0x0100short* r = (short*)&i; // r = 0x0100
0x0100(i)
p p + 1 p + 2p - 1
0x0104 0x01080x00fc
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer Arithmetic
int i = 123; // Addr of i is 0x0100int* p = &i; // p = 0x0100char* q = (char*)&i; // q = 0x0100short* r = (short*)&i; // r = 0x0100
0x0100(i)
q q + 1 q + 2q - 1
0x0104 0x01080x00fc
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer Arithmetic
int i = 123; // Addr of i is 0x0100int* p = &i; // p = 0x0100char* q = (char*)&i; // q = 0x0100short* r = (short*)&i; // r = 0x0100
0x0100(i)
r r + 1 r + 2r - 1
0x0104 0x01080x00fc
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Demo
Demo - PtrPlusPtr.cppRun PtrPlusPtr.cpp.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Demo
Demo - Endianness.cppRun Endianness.cpp.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer Subtraction
Subtraction 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.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer Subtraction
Example (Pointer Subtraction)int* p;int* q;int a = q - p;
p q
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer Subtraction
Example (Pointer Subtraction)int* p;int* q;int a = q - p;
p qq - p = 5
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Demo
Demo - PtrMinusPtr.cppRun PtrMinusPtr.cpp.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer Arithmetic
Exampleint a = (p2 - p1) + 5;int b = p2 - (p1 - 5);int c = (p2 + 5) - p1;
These are legal operations.Are they equivalent?
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointer Arithmetic
Example (Illegal Addition)int* mid = (p2 + p1)/2; // Midpt b/t p1, p2
Pointer addition is illegal.
How can we obtain a pointer to the object halfway betweenp1 and p2?
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointers and Constants
Example (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!
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
An Array Name as a Pointer
Example (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 assigned to a pointer of thesame type.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
A Pointer as an Array Name
Example (Pointers and Arrays)int a[3] = {10, 20, 30};int* pi = a;cout << pi[0] << endl;cout << pi[1] << endl;cout << pi[2] << endl;
A pointer name may be indexed.p[i] is equivalent to *(p + i).
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Demo
Demo - PointerLoop.cppRun PointerLoop.cpp.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Pointers as Function Parameters
In CS I, we learn to pass objects to function byreference.The intention is to allow the object to be modified by thefunction.Before pass-by-reference was introduced, the samewas accomplished by passing a pointer.To access the object form within the function, thepointer parameter must be dereferenced.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Example
Example (Pointer Parameter)void swap(int* a, int* b){
int temp = *a;
*a = *b;
*b = temp;return;
}
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Demo
Demo - IntegerInput.cppRun IntegerInput.cpp.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Arrays as Function Parameters
When an array is “passed” as a parameter, the name ofthe array is passed, as a pointer.Thus, the function receives a pointer to the first elementof the array.This is far more efficient than copying the entire array.
Pointers
Robb T.Koether
Introduction
PointerArithmeticPointer + int
Pointer - Pointer
Pointers andArrays
Pointers asFunctionParameters
Assignment
Example
Example (Array Parameter)void Sort(int list[], int size){
for (int i = 0; i < size - 1; i++){
int* q = list;while (q < list + size - 1){
if (*q > *(q + 1))Swap(q, q + 1);
q++;}
}return;
}