pointers and arrays - chapter 8bai/egc251/pointers and... · pointers and arrays c/c++ programming...

14
4/13/2020 Chapter 8 1 Chapter 8 Pointers and Arrays C/C++ Programming SUNY – New Paltz Elect. & Comp. Eng. SUNY – New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable that contains the address of a variable 2 Pointers are much used in C, because: Pointers are sometimes the only way to express a computation Pointers usually lead to more compact and efficient code Pointers and arrays are closely related 1 2

Upload: others

Post on 09-Aug-2020

12 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 1

Chapter 8Pointers and Arrays

C/C++ Programming

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 2

Pointers - Introduction

A pointer is a variable that contains the address of a variable

2

Pointers are much used in C, because:

Pointers are sometimes the only way to express a computation

Pointers usually lead to more compact and efficient code

Pointers and arrays are closely related

1

2

Page 2: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 2

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 3

Pointers - IntroductionBlock of Memory

Pointer: Contains the address of c

The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to

Therefore the statement x = *p; and x = c; are equivalent

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 4

Pointers - Exercise

Find the values of x, y, and z[0] through z[9] after the program ends

3

4

Page 3: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 3

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 5

Pointers Manipulations

Spring 2011 5Chapter 5

This means the location that dp points to is a

double

x=x+10;

y=x+1;

x=x+1;

++x

x++

ip = &x

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 6

Pointers and Function Arguments

6Chapter 8

Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function

Swaps ONLY copies of the arguments!

5

6

Page 4: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 4

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 7

Pointers and Function Arguments

Passes addresses of the two operands to function

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 8

Pointers and Arrays

7

8

Page 5: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 5

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 9

Pointers and Arrays

9

*pa a[0]

*(pa+1) a[1]

*(pa+2) a[2]

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 10

Pointers and Arrays

9

10

Page 6: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 6

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 11

Pointers and Arrays

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 12

Pointers and Arrays

Preferred Declaration

11

12

Page 7: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 7

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 13

Address Arithmetic

If p is a pointer to some element of an array, then:

p++ increments p to point to the next element

p+=i increments it to point i elements beyond where it currently does.

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 14

Address Arithmetic

If p and q point to members of the same array, then:

relations like ==, !=, <, >=, etc., work properly

For example, p < q is true if p points to an earlier element of the array than q does

13

14

Page 8: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 8

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 15

Character Pointers and Functions

char *pmessage;

pmessage = "now is the time";

assigns to pmessage a pointer to the character array. This is not a string copy; only pointers are involved.C does not provide any operators for processing an entire string of characters as a unit

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 16

Character Pointers and Functionsamessageis an array

pmessage is a pointer

15

16

Page 9: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 9

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 17

Character Pointers and Functions

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 18

Character Pointers and Functions

Preferred Code!

17

18

Page 10: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 10

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 19

Pointer Arrays; Pointers to Pointers

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 20

Multi-dimensional Arrays

19

20

Page 11: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 11

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 21

Multi-dimensional Arrays

Write a function that multiplies two n by n square matrices together.

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 22

Initialization of Pointer Arrays

21

22

Page 12: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 12

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 23

Pointers vs. Multi-dimensional Arrays

a is a true two-dimensional array: 200 int-sized locations have been set aside The conventional rectangular subscript calculation20 * row +col is used to find the element a[row,col]

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 24

Pointers vs. Multi-dimensional Arrays

b is an array of 10 pointers which are not initializedAssuming that each element of b does point to a twenty- element array, then there will be 200 ints set aside, plus ten cells for the pointers. The important advantage of the pointer array is that the rows of the array may be of different lengths. That is, each element of b need not point to a twenty-element vector; some may point to two elements, some to fifty, and some to none at all!

23

24

Page 13: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 13

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 25

Pointers vs. Multi-dimensional Arrays

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 26

Command-line Arguments

25

26

Page 14: Pointers and Arrays - Chapter 8bai/EGC251/Pointers and... · Pointers and Arrays C/C++ Programming SUNY –New Paltz Elect. & Comp. Eng. 2 Pointers - Introduction A pointer is a variable

4/13/2020

Chapter 8 14

SUNY – New PaltzElect. & Comp. Eng. SUNY – New PaltzElect. & Comp. Eng. 27

Command-line Arguments

27

27