pointers - city university of new york€¦ · 9 pointers – address operator address operator...

37
1 REVIEW REVIEW Pointers

Upload: others

Post on 24-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

1

REVIEWREVIEW

Pointers

Page 2: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

2

Purpose

Pointers enable programs

to simulate call-by-reference;

to create and manipulate dynamic data structures, i.e. data structures that can grow and shrink, such as linked lists, queues, stacks, and trees.

Page 3: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

3

Example 1

int *countPtr, count ;float *xPtr, *yPtr ;

count

7 count directly references a variable whose value is 7

7 .

countcountPtr countPtr indirectly references a variable whose value is 7

Fig. 1. Directly and indirectly referencing a variable

Page 4: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

4

Fig. 2. Accessing variables through pointers

Page 5: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

5

Fig. 3

Page 6: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

6

Fig. 4.

Page 7: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

7

Initialization of pointers

either when they are declared or in assignment statement. A pointer may be initialized to 0, NULL, or an address

Example:int y = 5 ;int *yPtr ;yPtr = &y ;

cout << *yPtr << endl ;cout << y << endl ;*yPtr = 9 ;cin >> *yPtr ;

yPtr y

600000 600000 5 500000

Fig. 5. Representation of y and yPtr in memory

Page 8: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

8

Initialization of pointers cont’d

Example:int *pi ;pi = 0xB8000000 ; // type mismatchint *p ;pi = (int *) 0xB8000000 ; // types now match

Page 9: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

9

Pointers – address operator

address operator &: returns the address of its operand:

Declaration and initializationint x = 27 ;

• allocate memory for int, referenced by x.• initialize with 27pointer – variable which contains the address of some other variable:

int *ptrx ; int *ptrx = &x;ptrx = &x ;

• ptrx is a variable with its own address (e.g. 1234).ptrx point to the variable x.

1234

5678x

ptrx 5678

27

Page 10: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

10

* - Unary indirection operator (dereference operator)

has one operand – address (pointer)access the contents of its operand (the contents of memory location, the pointer points at).

int y ;int *ptry ;y = *ptrx ;*ptry = *ptrx ;*ptry = x ;y = x ;

If ptrx is a pointer to integer, *ptrx can be used in any context where an integer could be found:

y = *ptrx + 1 ;

Page 11: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

11

Dereference operator

Pointers are variables and can be manipulated like other variables. int x ;int *ptrx, *ptry ; ptrx = &x ;

then:ptry = ptrx ;

copies the content of ptrx into ptry (ptrx and ptry point to x).

double x ; ptrx = &x ; // illegal - pointer to integer

Page 12: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

12

Page 13: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

13

Address and Indirection Operator

Pointer Variable Declaration

*(&x)

Page 14: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

14

Declaring Pointer Variables

Page 15: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

15

// Example: Demonstrate use of pointers

#include <iostream.h>int main (void) {// Local declarations

int a;int *p;

// Statementsa = 14;p = &a;cout << a << " " << &a << endl;cout << p << " " << *p << " " << a << endl;return 0;

} // main/*Results:

14 0x00d50a2a0x00d50a2a 14 14

*/

Page 16: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

16

Initialization of Pointer Variables

Uninitialized pointers- generate run-time error. C++ does not initialize variables (in general). OS often clears memory- not a rule.

Page 17: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

17

Initializing Pointer Variables

int *p = NULL ;

Page 18: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

18

// Example. Fun with pointers#include <iostream.h>int main (void){//Local Declarations

int a, b, c, *p, *q, *r ;// Statements

a = 6; b = 2; p = &b;q = p; r = &c; p = &a;*q = 8; c = *q; *r = *p;*r = a + *q + *&c;cout << a << " " << b << " " << c << endl;cout << *p << " " << *q << " " << *r << endl;return 0;

} // main/*Results:

6 8 206 8 20

*/

Page 19: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

19

Add two numbers using pointers

Page 20: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

20

// Example add.cpp. Add two numbers using pointers#include <iostream.h>int main (void) {//Local Declarations

int a; int b; int r;int* pa = &a; int* pb = &b; int* pr = &r;

// Statementscout << "\nEnter the first number: "; cin >> *pa;cout << "\nEnter the second number: "; cin >> *pb;*pr = *pa + *pb; cout << endl; cout << *pa << " + " << *pb << " is " << *pr << endl; return 0;

} // main// Results:// Enter the first number: 17// Enter the second number: 29// 17 + 29 is 46

Page 21: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

21

One pointer can be used to access several variables:

Page 22: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

22

// Example access.cpp. Using one pointer for many variables#include <iostream.h>#include <iomanip.h>int main ( void ) {//Local Declarations

int a; int b; int c; int *p;// Statements

cout << "\nEnter three numbers and key return: ";cin >> a >> b >> c;p = &a; cout << setw(3) << *p << endl;p = &b; cout << setw(3) << *p << endl;p = &c; cout << setw(3) << *p << endl;return 0;

} // main // Results:// Enter three numbers and key return: 5 8 -3// 5// 8// -3

Page 23: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

23

One Variable with many pointers

Page 24: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

24

// Example morepointers.cpp. Using one variable with many pointers

#include <iostream.h>int main (void) {//Local Declarations

int a; int *p = &a; int *q = &a; int *r = &a;// Statements

cout << "\nEnter a number: "; cin >> a;cout << *p << endl; cout << *q << endl; cout << *r << endl; return 0;

} // main// Results:// Enter a number: 17// 17// 17// 17

Page 25: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

25

Pointers and Functions

Page 26: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

26

Pointers and Functions cont’d

Page 27: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

27

Alternative to pass by reference – pass a pointer by value and use it to change the original variable.

Page 28: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

28

Functions, returning pointers

Page 29: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

29

there is no limit on how many levels of indirection can be used.

Pointers to pointers

Page 30: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

30

// Example pointers_to_pointers.cpp. Using pointers to pointers#include <iostream.h>int main (void) {// Local Declarations

int a; int *p; int **q; int ***r;// Statements

p = &a; q = &p; r = &q;cout << "Enter a number: "; cin >> a;cout << "Your number is: " << a << endl;cout << "\nEnter a number: "; cin >> *p;cout << "Your number is: " << a << endl;cout << "\nEnter a number: "; cin >> **q;cout << "Your number is: " << a << endl; cout << "\nEnter a number: "; cin >> ***r;cout << "Your number is: " << a << endl; return 0;

} // main /* Results:

Enter a number: 1Your number is: 1Enter a number: 2Your number is: 2Enter a number: 3Your number is: 3Enter a number: 4Your number is: 4

*/

Page 31: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

31

CompatibilityIn addition to its own attributes, pointers take on attributes of the type to

which it refers. Assign a pointer of one type to a different type – illegal. // P09-07 Demonstrate size of pointers#include <iostream.h>int main (void){// Local Declarations

char c; char *pc;int sizeofc = sizeof(c);int sizeofpc = sizeof(pc);int sizeofStarpc = sizeof(*pc);

int a; int *pa;int sizeofa = sizeof(a);int sizeofpa = sizeof(pa);int sizeofStarpa = sizeof(*pa);

double x; double *px;int sizeofx = sizeof(x);int sizeofpx = sizeof(px);int sizeofStarpx = sizeof(*px);

Page 32: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

32

// Statementscout << "sizeof(c): " << sizeofc << " | ";cout << "sizeof(pc): " << sizeofpc << " | ";cout << "sizeof(*pc): " << sizeofStarpc << endl;

cout << "sizeof(a): " << sizeofa << " | ";cout << "sizeof(pa): " << sizeofpa << " | ";cout << "sizeof(*pa): " << sizeofStarpa << endl;cout << "sizeof(x): " << sizeofx << " | ";cout << "sizeof(px): " << sizeofpx << " | ";cout << "sizeof(*px): " << sizeofStarpx << endl;return 0;

} // main /*Results:

sizeof(c): 1 | sizeof(pc): 4 | sizeof(*pc): 1sizeof(a): 4 | sizeof(pa): 4 | sizeof(*pa): 4sizeof(x): 8 | sizeof(px): 4 | sizeof(*px): 8

*/

Page 33: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

33

Void PointersVoid pointer can be used with any type, but cannot be

dereferenced. void *pVoid ;Casting:int a ; char *p ;p = (char*) &a ;Valid, but dangerous assignments:void *pVoid ; char *pChar ; int *pint ;pVoid = pChar ;pInt = pVoid ;pInt = (int*) pChar ;

Page 34: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

34

Assignment must be at the correct level

Page 35: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

35

Pointer types must match

Page 36: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

36

Assuming all variables are integer, and all pointers are of proper type,show the final values of the variables in the figure after:a=***p;s=**p;t=*p;b=**r; **q=b;

Page 37: Pointers - City University of New York€¦ · 9 Pointers – address operator address operator &: returns the address of its operand: Declaration and initialization intx = 27 ; •

37

Write a program that creates the structure shown in the figure and reads data into a and b, using pointers x and y. The program then multiplies the value of a by b and stores the result in c using pointers x, y and z. Finally, all three variable are printed using the pointers x, y, and z.