Pointers Lecture 2 Sections 10.3-10.8 Robb T. Koether Hampden-Sydney College Mon, Jan 20, 2014 Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 1 / 35
1 Endianness 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 2 / 35
Outline 1 Endianness 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 3 / 35
Big Endian and Little Endian Definition (Big Endian, Little Endian) If a processor has Big Endian architecture, then it stores the highest-order byte of the value in the lowest byte address. If a processor has Little Endian architecture, then it stores the lowest-order byte of the value in the lowest byte address. Multi-byte objects are addressed by their lowest byte address. Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 4 / 35
Big Endian and Little Endian Suppose we make the assignment int i = 1000000000; It so happens that 1000000000 10 = 3B9ACA00 16. If the architecture is Big Endian, then it is stored as "Big" end first 3B 9A CA 00 If the architecture is Little Endian, then it is stored as "Little" end first 00 CA 9A 3B Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 5 / 35
Example Example (Example) Endianness.cpp Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 6 / 35
Outline 1 Endianness 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 7 / 35
Outline 1 Endianness 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 8 / 35
Pointer + int Pointer + int int i = 123; int* p = &i; int* q = p + 1; p++; It is permissible to add an integer to a pointer. The integer is multiplied by the size of the object pointed to. That value is added to the address. The result is a pointer of the same type. Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 9 / 35
Pointer + int The integer is interpreted as the number of objects of that type between the original address and the new address. The resulting address is interpreted as a pointer to the object located the specified number of objects to the right, as in an array. Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 10 / 35
Pointer Arithmetic Pointer Arithmetic int i = 123; // Addr of i is 0x0100 int* p = &i; // p = 0x0100 char* q = (char*)&i; // q = 0x0100 short* r = (short*)&i; // r = 0x0100 0x0100 (i) Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 11 / 35
Pointer Arithmetic Pointer Arithmetic int i = 123; // Addr of i is 0x0100 int* p = &i; // p = 0x0100 char* q = (char*)&i; // q = 0x0100 short* r = (short*)&i; // r = 0x0100 p - 1 p p + 1 p + 2 0x00fc 0x0100 (i) 0x0104 0x0108 Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 12 / 35
Pointer Arithmetic Pointer Arithmetic int i = 123; // Addr of i is 0x0100 int* p = &i; // p = 0x0100 char* q = (char*)&i; // q = 0x0100 short* r = (short*)&i; // r = 0x0100 q - 1 q q + 1 q + 2 0x00fc 0x0100 (i) 0x0104 0x0108 Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 13 / 35
Pointer Arithmetic Pointer Arithmetic int i = 123; // Addr of i is 0x0100 int* p = &i; // p = 0x0100 char* q = (char*)&i; // q = 0x0100 short* r = (short*)&i; // r = 0x0100 r - 1 r r + 1 r + 2 0x00fc 0x0100 (i) 0x0104 0x0108 Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 14 / 35
Example Example (Example) Ptr Plus Int.cpp Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 15 / 35
Outline 1 Endianness 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 16 / 35
Pointer Subtraction Subtraction of one pointer from another pointer (of the same 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 type between two addresses. Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 17 / 35
Pointer Subtraction Pointer Subtraction int* p; int* q; int a = q - p; p q Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 18 / 35
Pointer Subtraction Pointer Subtraction int* p; int* q; int a = q - p; p q - p = 5 q Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 19 / 35
Example Example (Example) Ptr Minus Ptr.cpp Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 20 / 35
Pointer Arithmetic Pointer Arithmetic int a = (p2 - p1) + 5; int b = p2 - (p1-5); int c = (p2 + 5) - p1; These are legal operations. Are they equivalent? Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 21 / 35
Pointer Arithmetic Illegal Addition int* mid = (p2 + p1)/2; Pointer addition is illegal. How can we obtain a pointer to the object halfway between p1 and p2? Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 22 / 35
Outline 1 Endianness 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 23 / 35
Pointers and Constants Pointers and Constants int const i = 123; int j = 456; int const * pi1 = &i; int * const pi2 = &j; int const * const pi3 = &i; A pointer may point to a constant the object pointed to cannot be changed. A pointer itself may be constant the pointer cannot be changed. In fact, a constant pointer may point to a constant object! Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 24 / 35
An Array Name as a Pointer Pointers and Arrays int a[3] = {10, 20, 30}; int* iptr = a; cout << *iptr << endl; cout << *(iptr + 1) << endl; cout << *(iptr + 2) << endl; An array name represents a pointer that points to the first member of the array. The array name is a constant pointer; its value cannot be changed. An array name may be assigned to a pointer of the same type. Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 25 / 35
A Pointer as an Array Name 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). Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 26 / 35
Example Example (Example) Pointer Loop.cpp Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 27 / 35
Outline 1 Endianness 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 28 / 35
Pointers as Function Parameters In CS I, we learn to pass objects to function by reference. The intention is to allow the object to be modified by the function. Before pass-by-reference was introduced, the same was accomplished by passing a pointer. To access the object form within the function, the pointer parameter must be dereferenced. Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 29 / 35
Example Pointer Parameter void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; return; } Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 30 / 35
Example Example (Example) Pass By Pointer.cpp Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 31 / 35
Arrays as Function Parameters When an array is passed as a parameter, the name of the array is passed, as a pointer. Thus, the function receives a pointer to the first element of the array. This is far more efficient than copying the entire array. Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 32 / 35
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; } Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 33 / 35
Outline 1 Endianness 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 34 / 35
Assignment Homework Read Sections 10.3-10.8. Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 35 / 35