Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.

Similar presentations


Presentation on theme: "Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory."— Presentation transcript:

1 Programming Pointers

2 COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory addresses

3 COMP104 Lecture 32 / Slide 3 Pointers l Usefulness n 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

4 COMP104 Lecture 32 / Slide 4 Basics l Pointer n Object whose value represents the location (memory address) of another object n 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 ) n Even pointers to pointers –Pointers to pointers to int objects

5 COMP104 Lecture 32 / Slide 5 Syntax l 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 l 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”

6 COMP104 Lecture 32 / Slide 6 t points to address of a character i 'y'c 1 t ptr Memory Depiction int i = 1; char c = 'y'; int *ptr = &i; char *t; t = &c

7 COMP104 Lecture 32 / Slide 7 Address Operator l 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;

8 COMP104 Lecture 32 / Slide 8 Indirection Operator An asterisk (“*”) has two uses for pointers n 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 n 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 "

9 COMP104 Lecture 32 / Slide 9 Null Address 0 (NULL) is a pointer constant that represents the empty or null address n Indicates that pointer is not pointing to storage of a valid object n 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

10 COMP104 Lecture 32 / Slide 10 Member Indirection l Consider Rational r(4,3); Rational *rPtr = &r; To select a member of r through indirection using rPtr, operator precedence requires we do the following (*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)

11 COMP104 Lecture 32 / Slide 11 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; }

12 COMP104 Lecture 32 / Slide 12 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; }

13 COMP104 Lecture 32 / Slide 13 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 1 2 3 4 0 arr arr+1 arr+2 arr+3 arr+4 ptr


Download ppt "Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory."

Similar presentations


Ads by Google