1 / 77

Pointers (Chapter 9)

int a;. Pointers (Chapter 9). A pointer keeps the address of a memory cell that is used for storing and accessing data. Why Pointers?. Curious – like to know where exactly is your variable in memory, the content of a certain memory location.

Download Presentation

Pointers (Chapter 9)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. int a; Pointers (Chapter 9) A pointer keeps the address of a memory cell that is used for storing and accessing data COMP103 - Pointers

  2. Why Pointers? • Curious – like to know • where exactly is your variable in memory, • the content of a certain memory location. • Not sure how much memory you need for your program int A[size]; COMP103 - Pointers

  3. 000000 000001 000002 Computer Memory • Memory can be conceptualized as a linear set of “cells” (one byte in size) with a number, referred to as “address” associated with each cell. Variables Memory cells Address achar a COMP103 - Pointers

  4. Computer Memory 0 0 1 0 4 bytes h e l l o 5 single bytes Variable Data and Memory Variables’ data are stored in these cells int a = 10; charmystr[5]=“hello”; Note: Different types of data require different amounts of memory COMP103 - Pointers

  5. Computer Memory 0 0 2 0 4 bytes h e l l o 5 single bytes Variable Data and Memory Variables’ data are stored in these cells char mystr[]=“hello”; int a = 10; a = 20; Variable a is bound to this memory location! An assignment changes the data in the memory location COMP103 - Pointers

  6. 000000 000001 004000 To 004003 0 0 1 0 1048573 1048574 1048575 Pointer Constants (or memory address) Pointer Constants We can think memory addresses as Pointer Constants int a = 10; COMP103 - Pointers

  7. 000000 000001 004000 To 004003 0 0 1 0 1048573 &a004000 1048574 1048575 Address Operator (&) Address operator (&) provides a pointer constant of the variable: usage &variable_name Pointer Constants int a = 10; COMP103 - Pointers

  8. Address Operator & • &variable gives the address of the variable • &a and &b. • Result: 142300 142301 (note: this does not work in Visual C++!!) Variable name Address Figure 9-4 (p.415) COMP103 - Pointers

  9. Pointer Constants 000000 000001 004000 To 004003 0 0 1 0 a p 0 0 0 4 1048573 1048574 1048575 Pointer Variable • A pointer variable is a variable that stores a memory location (address) • Pointers are declared using a “*” after the type int a = 10; // a pointer variable!!! int *p; p = &a; COMP103 - Pointers

  10. Pointer Variable Declaration Figure 9-10 (p.419) Type of data at the Memory location Variable Name COMP103 - Pointers

  11. Declaring Pointer Variables Figure 9-11 (p.419) COMP103 - Pointers

  12. Pointer Constants 000000 000001 004000 To 004003 0 0 1 0 a p 0 4 0 0 1048573 1048574 1048575 Don’t get confused Pointer variable, p • does not hold the value of a • pointsto the memory location of a • is a variable itself • has its own memory location (1002300) int a; int *p; a=10; P = &a; 1002300 COMP103 - Pointers

  13. Multiple Pointers to a Variable • We may have multiple pointer variables pointing to the same memory cell! • What are their values? • a = ? • &a = ? • p = ? • q = ? Figure 9-7 (p.417) COMP103 - Pointers

  14. Multiple Pointers to One Variable • How? int a, *p, *q, *r; p = &a; q = &a; r = q; Figure 9-6 (p.425) COMP103 - Pointers

  15. Multiple Pointers to a Variable int x; int *p, *q; p = &x; q = &x; Figure 9-8 (p.418) Changing the variable x does not affect the pointers p and q COMP103 - Pointers

  16. Un-initialized Pointers Figure 9-12 (p.421) COMP103 - Pointers

  17. Initialize Pointer Variables • int *p = &x; // set a pointer to nothing • int *p = NULL; Figure 9-13 (p.421) COMP103 - Pointers

  18. Multiple Bindings • The power of pointers! • A pointer may be used to access different variables. p = &b p = &c p = &a Figure 9-15 (p.424) COMP103 - Pointers

  19. Address a 100203 p 100203 Using pointers - Indirection (*) We can use the pointer variable to access the variable it “points” to by • Placing a * before a pointer variable, p  refers to the content of the address of the variable given in p. int a, *p; a=5; p = &a; cout << p << *p; Result: 100203 5 • This is sometimes called “ de-referencing” the pointer 5 COMP103 - Pointers

  20. Address Operator (&) and Indirection (*) Address Operator Indirection & * inverse operators &a reads “give me the memory address of the variable a” *p reads “give me the content of the memory address which is stored in the pointer variable p” Example: int x=5; cout << *(&x); // *(&x) is just x COMP103 - Pointers

  21. Indirection Operator * • Using the indirection operator * • Assume: p = &x; • Possible ways of adding 1 to variable x: • x++; • x = x + 1; • *p = *p + 1; COMP103 - Pointers

  22. Add Two Numbers using Pointers • How do we achieve the following effects using pointers? Figure 9-14 (p.423) r = a + b COMP103 - Pointers

  23. More Examples • Assume: int *p, *q; p = &x; q = &x; Figure 9-8 (p.418) COMP103 - Pointers

  24. Dereferencing garbage pointers int a = 0; int *p; a = a + *p; // What will happen???? COMP103 - Pointers

  25. References vs. Pointers SUPERMAN (same person [reference]) CLARK KENT PERSON (Pointing to Superman [two different people]) *PERSON = Superman (What is he pointing to? Superman) COMP103 - Pointers

  26. a a p memory num References versus Pointers • Reference and pointers are different 6 6 A pointer is a variable. It points to a memory location. You have to use the (*) indirection operator to access the memory location at ‘a’; int a; int *p = &a; Cout << *p // same a A reference shares the same memory location with other variables. int a; int &num = a; // num ISa COMP103 - Pointers

  27. Parameter Passing - by Value • Variables a and x use different memory cells. different copies Figure 9-17(a) (p.426) COMP103 - Pointers

  28. Parameter Passing - by Reference • Variables a and x share the same memory cell. same copy Figure 9-17(b) (p.426) COMP103 - Pointers

  29. Parameter Passing - by Pointers • Pointer variables,px and py refer to the memory cells of a and b. Figure 9-17(c) (p.417) COMP103 - Pointers

  30. Pointers as Formal Parameters • When we use pointer variables as formal parameters, we achieve the same effects of parameter passing by reference. by-reference: void exchange(int &x, int &y) by-pointer: void exchange(int *px, int *py) COMP103 - Pointers

  31. Functions Returning Pointers Figure 9-18 (p.427) COMP103 - Pointers

  32. Note in returning pointers • Never return a pointer to a local variable. You’ll get a warning message • Must point to a variable in the calling function void main() { int a, b, *p; … p = smaller( &a, &b ); … } int *smaller (int *px, int *py) { int temp = (*px < *py)? *px : *py; return (&temp); } This is bad! Don’t do it. COMP103 - Pointers

  33. Pointer Indirection (Pointers to Pointers) What is a? &a? *a? p? &p? *p? **p? q? &q? *q? **q? Figure 9-19 (p.428) 58 COMP103 - Pointers

  34. Pointers Indirection • How to access a from p? *p • How to access a from q? **q • How to access a from r? ***r 58 Figure 9-20 (p.429) COMP103 - Pointers

  35. Casting Pointers (DO NOT DO!) • What if we want to have pc pointing to a? • Warning: You're advised not to cast pointers unless absolutely necessary. Figure 9-21 (p.433) pc = (char *) &a; COMP103 - Pointers

  36. Pointer Types Must Match Figure 9-22 (p.433) COMP103 - Pointers

  37. Lvalue vs Rvalue (Ch 9-10,p.434-436) • An C++ expression is either a rvalue or lvalue. • A rvalue expression appears at the right of an assignment. It refers to a value that be assigned to a memory cell, i.e. can be used to supply a value for further use, e.g. examine or copy the value. • Examples: x= 5; y= a+2; z=a*6; x=a[2]+3; i=i++; • A lvalue expression appears at the left of an assignment. It identifies a memory cell that is going to receive a rvalue, i.e. the memory cell is being modified. • Example: a = … a[5] = … (a) = … *p = … COMP103 - Pointers

  38. Arrays and Pointers • The name of an array points only to the first element not the whole array, i.e. the variable name of an array is a pointer variable. Figure 9-25 (p.443) COMP103 - Pointers

  39. Array Name is a pointer constant Example on p.443 of text (where is the array in memory?) #include <iostream.h> voidmain() { // Demonstrate array name is a pointer constant int a[5]; cout << "Address of a[0]: " << &a[0] << endl << "Name as pointer: " << a << endl; } /* result: Address of a[0]: 0x0065FDE4 Name as pointer: 0x0065FDE4 */ COMP103 - Pointers

  40. Dereference of An Array Name How to obtain the value of a[0] using pointer operator, “*”? Figure 9-26 (p.444) COMP103 - Pointers

  41. Array Names as Pointers To access an array, any pointer to the first element can be used instead of the name of the array. We could replace *p by *a Figure 9-27 (p.444) COMP103 - Pointers

  42. Multiple Array Pointers • Both a and p are pointers to the same array. Figure 9-28 (p.445) COMP103 - Pointers

  43. Pointer Arithmetic • Given a pointer p, p+n refers to the element that is offset from p by n positions. Figure 9-29 (p.446) COMP103 - Pointers

  44. Pointer Arithmetic & Different Types • address = pointer + (offset * size of element) Figure 9-30 (p.446) COMP103 - Pointers

  45. Dereferencing Array Pointers Figure 9-31 (p.447) *(a+n) is identical to a[n] COMP103 - Pointers

  46. Pointers—Arrays Duality • int a[10]; • You can think of ‘a’ as really a pointer. • a[0] = *(a+0) = memory at (a+0) • a[1] = *(a+1) = memory at (a+1) • a[2] = *(a+2) = memory at (a+2) COMP103 - Pointers

  47. Example: Find Smallest (Figure 9-32, p.447) Figure 9-32 (p.447) COMP103 - Pointers

  48. Pointer to 2-Dimensional Arrays What is **table? table[i][j] Figure 9-33 (p.450) COMP103 - Pointers

  49. Passing an Array to a Function • Caller program: • func( arrayName, size ); • Called program: • int func( int arr [ ], int size ) {…} // preferred or • int func( int *arr, int size ) {…} // same effect • Arrays are passed to a function by reference. COMP103 - Pointers

  50. Variables for Multiplying Array Elements by Two Figure 9-34 (p.452) Program 9-12 (p.452) COMP103 - Pointers

More Related