scis.regis.edu ● cs-362: data structures week 6 part 2 dr. jess borrego 1
DESCRIPTION
Pointer Data Type/Pointer Variables The type is integer The Name of the integer is A The address is The value stored in A is 325 int A = 325;10280 A 3 325TRANSCRIPT
![Page 2: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/2.jpg)
Topics•Dynamic Data
▫Pointers▫Address
•Pointers to different data types•Sample programs
2
![Page 3: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/3.jpg)
Pointer Data Type/Pointer Variables•The type is integer•The Name of the integer is A•The address is 10280•The value stored in A is 325
int A = 325; 10280
A
3
325
![Page 4: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/4.jpg)
More Pointers
4
![Page 5: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/5.jpg)
Running the program
5
![Page 6: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/6.jpg)
Running the program
6
![Page 7: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/7.jpg)
PointerAndAddress
7
![Page 8: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/8.jpg)
Declaring Pointer Variables• Syntax:
• Examples:int *p;char *ch;
• These statements are equivalent:int *p;int* p; int * p;
8
![Page 9: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/9.jpg)
Pointer Intro Program (I)
9
![Page 10: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/10.jpg)
Pointer Intro Program (II)
10
![Page 11: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/11.jpg)
Running the program
11
![Page 12: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/12.jpg)
Pointer Intro Program (I)
12
![Page 13: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/13.jpg)
Pointer Intro Program (II)
13
![Page 14: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/14.jpg)
Pointer Intro Program (II)
14
![Page 15: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/15.jpg)
MoreStruct
15
![Page 16: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/16.jpg)
Running the program
16
![Page 17: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/17.jpg)
MoreBeatles (I)
17
![Page 18: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/18.jpg)
MoreBeatles (II)
18
![Page 19: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/19.jpg)
MoreBeatles (III)
19
![Page 20: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/20.jpg)
Running the program
20
![Page 21: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/21.jpg)
PointerSwap
21
![Page 22: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/22.jpg)
Running the Program
22
![Page 23: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/23.jpg)
What happened?•The swap did not work?•The before and after are the same•Need to make sure we get the values back
to the calling program•It is calling by Value•How do we fix it?
23
![Page 24: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/24.jpg)
Address of Operator (&)•The ampersand, &, is called the address of
operator•The address of operator is a unary
operator that returns the address of its operand
24
![Page 25: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/25.jpg)
PointerSwap2
25
![Page 26: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/26.jpg)
PointerSwap3
26
![Page 27: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/27.jpg)
Running the program (I)
27
![Page 28: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/28.jpg)
Running the program (II)
28
![Page 29: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/29.jpg)
Dynamic Arrays•Dynamic array: array created during
the execution of a program•Example:int *p;p = new int[10];
*p = 25;p++; //to point to next array component*p = 35;
29
stores 25 into the first memory location
stores 35 into the second memory location
![Page 30: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/30.jpg)
Dynamic Arrays (cont'd.)•C++ allows us to use array notation to
access these memory locations•The statements:p[0] = 25;p[1] = 35;store 25 and 35 into the first and second array components, respectively
30
![Page 31: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/31.jpg)
Dynamic Arrays (cont'd.)
31
![Page 32: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/32.jpg)
Dynamic Arrays (cont'd.)
32
![Page 33: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/33.jpg)
Dynamic Arrays (cont'd.)• The value of list (1000) is constant
– Cannot be altered during program execution– The increment and decrement operations
cannot be applied to list• If p is a pointer variable of type int, then:p = list;copies the value of list, the base address of the array, into p– We can perform ++ and -- operations on p
• An array name is a constant pointer
33
![Page 34: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/34.jpg)
34
Dynamic Arrays (cont'd.)
![Page 35: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/35.jpg)
Functions and Pointers•A pointer variable can be passed as a
parameter either by value or by reference•To make a pointer a reference parameter
in a function heading, use &:void pointerParameters(int* &p, double *q){
. . .}
35
![Page 36: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/36.jpg)
Pointers and Function Return Values•A function can return a value of type
pointer:
int* testExp(...){
. . .}
36
![Page 37: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/37.jpg)
Dynamic Two-Dimensional Arrays•You can create dynamic multidimensional
arrays•Examples:
37
declares board to be an array of four pointers wherein each pointer is of type int
creates the rows of board
declares board to be a pointer to a pointer
![Page 38: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/38.jpg)
PointerArray
38
![Page 39: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/39.jpg)
PointerArray (Cont’d)
39
![Page 40: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/40.jpg)
Running the program
40
![Page 41: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/41.jpg)
PrintArray2D
41
![Page 42: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/42.jpg)
PrintArray2D (Cont’d)
42
![Page 43: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/43.jpg)
PrintArray (Cont’d)
43
![Page 44: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/44.jpg)
Running the Program
44
![Page 45: Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jess Borrego 1](https://reader035.vdocument.in/reader035/viewer/2022062909/5a4d1baf7f8b9ab0599cbe9c/html5/thumbnails/45.jpg)
Resources•Can search the internet for tutorials on
pointers•On YouTube:•http://www.youtube.com/watch?v=eFWCQ
exfaeg•From Google, search for
▫“C++ pointer tutorial“
45