32
Computer Science: A Structured Programming Approach Using C 1 Objectives To understand the relationship between arrays and pointers To understand the design and concepts behind pointer arithmetic To write programs using arrays and pointer arithmetic To better understand the design behind passing arrays to functions To understand the C implementation of dynamic memory To write programs using static and dynamic memory allocation Chapter 10 Chapter 10 Pointer Applications Pointer Applications

Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Embed Size (px)

DESCRIPTION

Computer Science: A Structured Programming Approach Using C3 FIGURE 10-1 Pointers to Arrays

Citation preview

Page 1: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 1

Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write programs using arrays and pointer arithmetic ❏ To better understand the design behind passing arrays to

functions ❏ To understand the C implementation of dynamic memory ❏ To write programs using static and dynamic memory allocation ❏ To understand and implement ragged arrays (arrays of pointers)

Chapter 10Chapter 10 Pointer ApplicationsPointer Applications

Page 2: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 2

10-1 Arrays and Pointers

The name of an array is a pointer constant to the first The name of an array is a pointer constant to the first element. Because the array’s name is a pointer element. Because the array’s name is a pointer constant, its value cannot be changed. Since the array constant, its value cannot be changed. Since the array name is a pointer constant to the first element, the name is a pointer constant to the first element, the address of the first element and the name of the array address of the first element and the name of the array both represent the same location in memory.both represent the same location in memory.

Page 3: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 3

FIGURE 10-1 Pointers to Arrays

Page 4: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 4

same a &a[0]a is a pointer only to the first element—not the whole array.

NoteNote

Page 5: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 5

The name of an array is a pointer constant;it cannot be used as an lvalue.

NoteNote

Page 6: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 6

FIGURE 10-2 Dereference of Array Name

Page 7: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 7

FIGURE 10-3 Array Names as Pointers

Page 8: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 8

FIGURE 10-4 Multiple Array Pointers

Page 9: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 9

To access an array, any pointer to the first element can be used instead of the name of the array.

NoteNote

Page 10: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 10

10-2 Pointer Arithmetic and Arrays

Besides indexing, programmers use another powerful Besides indexing, programmers use another powerful method of moving through an array: pointer method of moving through an array: pointer arithmetic. Pointer arithmetic offers a restricted set of arithmetic. Pointer arithmetic offers a restricted set of arithmetic operators for manipulating the addresses in arithmetic operators for manipulating the addresses in pointers. pointers.

Pointers and One-Dimensional ArraysArithmetic Operations on PointersUsing Pointer ArithmeticPointers and Two-Dimensional Arrays

Topics discussed in this section:Topics discussed in this section:

Page 11: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 11

Given pointer, p, p ± n is a pointer to the value n elements away.

NoteNote

Page 12: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 12

FIGURE 10-5 Pointer Arithmetic

Page 13: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 13

NoteNote

a + n * (sizeof (one element))

a + n

Page 14: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 14

FIGURE 10-6 Pointer Arithmetic and Different Types

Page 15: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 15

FIGURE 10-7 Dereferencing Array Pointers

Page 16: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 16

The following expressions are identical.*(a + n) and a[n]

NoteNote

Page 17: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 17

Table 10-1 Pointers and Relational Operators

Page 18: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 18

FIGURE 10-8 (Part I) Find Smallest

Page 19: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 19

FIGURE 10-8 (Part II) Find Smallest

Page 20: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 20

PROGRAM 10-1 Print Array with Pointers

Page 21: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 21

PROGRAM 10-1 Print Array with Pointers

Page 22: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 10-2 Pointers and the Binary Search

Page 23: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 10-2 Pointers and the Binary Search

Page 24: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 24

FIGURE 10-9 Pointers to Two-dimensional Arrays

Page 25: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 25

We recommend index notation for two-dimensional arrays.

NoteNote

Page 26: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 26

10-3 Passing an Array to a FunctionNow that we have discovered that the name of an Now that we have discovered that the name of an array is actually a pointer to the first element, we can array is actually a pointer to the first element, we can send the array name to a function for processing. send the array name to a function for processing. When we pass the array, we do not use the address When we pass the array, we do not use the address operator. Remember, the array name is a pointer operator. Remember, the array name is a pointer constant, so the name is already the address of the first constant, so the name is already the address of the first element in the array.element in the array.

Page 27: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 27

FIGURE 10-10 Variables for Multiply Array Elements By 2

Page 28: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 28

PROGRAM 10-3 Multiply Array Elements by 2

Page 29: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 10-3 Multiply Array Elements by 2

Page 30: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 10-3 Multiply Array Elements by 2

Page 31: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 31

PROGRAM 10-3 Multiply Array Elements by 2

Page 32: Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the

Computer Science: A Structured Programming Approach Using C 32