130 likes | 263 Views
Programming. Pointers. Pointers. Pointers are objects whose values are the locations of other objects Pointers are memory addresses. Pointers. Usefulness Necessary for dynamic objects Objects whose memory is allocated during program execution.
E N D
Programming Pointers
Pointers • Pointers are objects whose values are the locations of other objects • Pointers are memory addresses
Pointers • Usefulness • Necessary for dynamic objects • Objects whose memory is allocated during program execution. • Dynamic objects can survive after the function ends in which they were allocated. • Dynamic objects allow flexible-sized arrays
Basics • Pointer • Object whose value represents the location (memory address) of another object • C++ has pointer types for every type of object • Pointers to int objects • Pointers to char objects • Pointers to user-defined objects (e.g., Temperature) • Even pointers to pointers • Pointers to pointers to int objects
Syntax • Examples of uninitialized pointers int *iPtr; // iPtr is a pointer to an int char *s; // s is a pointer to a char Rational *rPtr; // rPtr is a pointer to a // Rational • Examples of initialized pointers int i = 1; char c = 'y'; int *ptr; // ptr is an int pointer ptr = &i; // ptr is assigned the address of i char *t; // t is a char pointer t = &c; // t is assigned the address of c Indicates pointer object “&i” means “the address of i”
Memory Depiction i 1 int i = 1; char c = 'y'; int *ptr = &i; char *t; t = &c t points to c 'y' address of t a ptr character
Address Operator • Another example: int i = 1; int j = 2; int *ptr; ptr = &i; // ptr points to location of i *ptr = 3; // contents of i are updated ptr = &j; // ptr points to location of j *ptr = 4; // contents of j are updated cout << i << " " << j << endl;
Indirection Operator • An asterisk (“*”) has two uses for pointers • We have already seen that in a declaration an asterisk indicates that the object is a pointer. char *s; // s is of type pointer to char • In expressions, an asterisk before a pointer indicates that we want to dereference the pointer (“content of” operator ). int i = 1, j; int *ptr; // ptr is an int pointer ptr = &i; // ptr points to i j = *ptr + 1; // j is assigned 2 cout << *ptr << j << endl; // display "12"
Null Address • 0 (NULL) is a pointer constant that represents the empty or null address • Indicates that pointer is not pointing to storage of a valid object • Cannot dereference a pointer (refer to the object pointed at by a pointer) whose value is null int *ptr; ptr = 0; cout << *ptr << endl; // invalid, ptr // does not point to // a valid int
Member Indirection • Consider Rational r(4,3); Rational *rPtr = &r; • To select a member of r through indirection using rPtr, operator precedence requires we do thefollowing (*rPtr).Display(); • This looks strange, so C++ provides the indirect member selector operator -> rPtr->Display(); Calls member Display of the object to which rPtr points (r) Calls member Display of the object to which rPtr points (r)
Traditional Pointer Usage in C void IndirectSwap(char *Ptr1, char *Ptr2) { char c = *Ptr1; *Ptr1 = *Ptr2; *Ptr2 = c; } int main() { char a = 'y'; char b = 'n'; IndirectSwap(&a, &b); cout << a << b << endl; return 0; }
Pass by Reference in C++ void IndirectSwap(char& y, char& z) { char temp = y; y = z; z = temp; } int main() { char a = 'y'; char b = 'n'; IndirectSwap(a, b); cout << a << b << endl; return 0; }
0 1 2 ptr 3 4 Example int arr[5], *ptr, *ptr2, j; for (int i=0;i<5;i++) arr[i] = i; ptr = arr; cout << *(ptr+3) << endl; // 3 cout << *ptr << endl; // 0 ptr++; cout << *ptr << endl; // 1 ptr += 2; cout << *ptr << endl; // 3 j = *ptr * 10; cout << j << endl; // 30 ptr2 = ptr - 2; cout << *ptr2 << endl; // 1 cout << ptr + 1 << endl; // address arr arr+1 arr+2 arr+3 arr+4