780 likes | 944 Views
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.
E N D
int a; Pointers (Chapter 9) A pointer keeps the address of a memory cell that is used for storing and accessing data COMP103 - Pointers
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
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
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
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
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
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
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
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
Pointer Variable Declaration Figure 9-10 (p.419) Type of data at the Memory location Variable Name COMP103 - Pointers
Declaring Pointer Variables Figure 9-11 (p.419) COMP103 - Pointers
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
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
Multiple Pointers to One Variable • How? int a, *p, *q, *r; p = &a; q = &a; r = q; Figure 9-6 (p.425) COMP103 - Pointers
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
Un-initialized Pointers Figure 9-12 (p.421) COMP103 - Pointers
Initialize Pointer Variables • int *p = &x; // set a pointer to nothing • int *p = NULL; Figure 9-13 (p.421) COMP103 - Pointers
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
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
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
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
Add Two Numbers using Pointers • How do we achieve the following effects using pointers? Figure 9-14 (p.423) r = a + b COMP103 - Pointers
More Examples • Assume: int *p, *q; p = &x; q = &x; Figure 9-8 (p.418) COMP103 - Pointers
Dereferencing garbage pointers int a = 0; int *p; a = a + *p; // What will happen???? COMP103 - Pointers
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
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
Parameter Passing - by Value • Variables a and x use different memory cells. different copies Figure 9-17(a) (p.426) COMP103 - Pointers
Parameter Passing - by Reference • Variables a and x share the same memory cell. same copy Figure 9-17(b) (p.426) COMP103 - Pointers
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
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
Functions Returning Pointers Figure 9-18 (p.427) COMP103 - Pointers
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
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
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
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
Pointer Types Must Match Figure 9-22 (p.433) COMP103 - Pointers
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
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
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
Dereference of An Array Name How to obtain the value of a[0] using pointer operator, “*”? Figure 9-26 (p.444) COMP103 - Pointers
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
Multiple Array Pointers • Both a and p are pointers to the same array. Figure 9-28 (p.445) COMP103 - Pointers
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
Pointer Arithmetic & Different Types • address = pointer + (offset * size of element) Figure 9-30 (p.446) COMP103 - Pointers
Dereferencing Array Pointers Figure 9-31 (p.447) *(a+n) is identical to a[n] COMP103 - Pointers
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
Example: Find Smallest (Figure 9-32, p.447) Figure 9-32 (p.447) COMP103 - Pointers
Pointer to 2-Dimensional Arrays What is **table? table[i][j] Figure 9-33 (p.450) COMP103 - Pointers
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
Variables for Multiplying Array Elements by Two Figure 9-34 (p.452) Program 9-12 (p.452) COMP103 - Pointers