Pointers Lecture 1 Sections 10.1-10.2 Robb T. Koether Hampden-Sydney College Wed, Jan 14, 2015 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 1 / 23
1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 2 / 23
Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 3 / 23
Pointers Definition (Pointer) A pointer is an object that holds the address of another object. All pointers occupy 4 bytes of memory. 4 bytes is sufficient to access 4GB of memory. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 4 / 23
Pointers : 2508 123 Pointer : 2516 2512 2508 2504 2500 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 5 / 23
Declaring Pointers Pointer Declaration int* ptri; float* ptrf; To declare a pointer, write the type of object that the pointer points to, followed by *, followed by the pointer name. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 6 / 23
Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 7 / 23
Null Pointers Pointer Initialized to NULL int* ptri = NULL; A null pointer is a pointer that has the value zero. A null pointer does not point to any object. The constant NULL has the value 0. It is a good practice to initialize a pointer to NULL if it will not immediately point to an object. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 8 / 23
Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 9 / 23
The Address Operator Pointer Initialized to an Object int i; int* ptri = &i; // &i is address of i The unary prefix address operator & returns the address of an object. The address may be assigned to a pointer. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 10 / 23
Example Example (Example) Address Operator.cpp Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 11 / 23
Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 12 / 23
Dereferencing Pointers Dereferencing Pointers int i = 10; int* ptri = &i; cout << *ptri << endl; // ptri points to i // Print i The unary prefix dereference operator * is used to dereference a pointer. When a pointer is dereferenced, it returns the object that it points to. Never, ever dereference a null pointer. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 13 / 23
Example Example (Example) Dereference Pointers.cpp Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 14 / 23
Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 15 / 23
Pointers to void A pointer to void, i.e., a void* object, is a pointer that does not point to any particular kind of object. There is no such thing as a void object. void is treated as a neutral type for situations where the type does not matter or is unknown. A void pointer may hold the address of any type of object. A void pointer may not be dereferenced. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 16 / 23
Assignment of Pointers The pointer assignment ptr1 = ptr2 is legal if and only if ptr1 and ptr2 point to the same type of object, or ptr2 is NULL, or ptr1 is a pointer to void. In all other cases, we would have to cast p2 to be compatible with ptr1. Assigning pointers to different types can cause serious problems. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 17 / 23
Pointer Compatibility Pointer Compatibility int i = 123; float f = 4.56; int* ptri = &i; float* ptrf = &f; // Swap ptri and ptrf void* temp = ptri; ptri = (int*)ptrf; ptrf = (float*)ptri; What are the values of *ptri and *ptrf? Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 18 / 23
Pointer Compatibility &i ptri &f ptrf 0x0000007b 0x4091eb85 i = 123 f = 4.56 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 19 / 23
Pointer Compatibility &i ptri &f ptrf 0x0000007b 0x4091eb85 i = 123 f = 4.56 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 20 / 23
Example Example (Example) Ptr Compatibility.cpp Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 21 / 23
Outline 1 Pointers 2 Pointer Initialization 3 The Address Operator 4 Dereferencing Pointers 5 Pointer Compatibility 6 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 22 / 23
Assignment Homework Read Sections 10.1-10.2. Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 23 / 23