Transcript
Page 1: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

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 10Chapter 10 Pointer ApplicationsPointer Applications

Page 2: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

2

10-1 Arrays and Pointers

The name of an array is a The name of an array is a pointer constant pointer constant to the first 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: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

3

FIGURE 10-1 Pointers to Arrays

Page 4: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

4

same

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

NoteNote

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

NoteNote

Page 5: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

5

FIGURE 10-2 Dereference of Array Name

Page 6: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

6

FIGURE 10-3 Array Names as Pointers

Page 7: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

7

FIGURE 10-4 Multiple Array Pointers

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

NoteNote

4

Page 8: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

8

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 9: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

9

FIGURE 10-5 Pointer Arithmetic

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

NoteNote

Page 10: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

10FIGURE 10-6 Pointer Arithmetic and Different Types

a + n * (sizeof (one element))

a + n

NoteNote

Page 11: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

11

FIGURE 10-7 Dereferencing Array Pointers

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

NoteNote

Page 12: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

12

Table 10-1 Pointers and Relational Operators

Page 13: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

13

FIGURE 10-8 (Part I) Find Smallest

Page 14: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

14

FIGURE 10-8 (Part II) Find Smallest

Page 15: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

15

PROGRAM 10-1

Print Array with Pointers

Page 16: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

16

FIGURE 10-9 Pointers to Two-dimensional Arrays

Page 17: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

17

10-3 Passing an Array to a Function

Now 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 18: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

18

FIGURE 10-10 Variables for Multiply Array Elements By 2

Page 19: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

19

PROGRAM 10-3

Multiply Array Elements by 2

Page 20: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

20

PROGRAM 10-3

Multiply Array Elements by 2

Page 21: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

21

10-4 Memory Allocation Functions

C gives us two choices when we want to reserve C gives us two choices when we want to reserve memory locations for an object: static allocation and memory locations for an object: static allocation and dynamic allocation. dynamic allocation.

Memory UsageStatic Memory AllocationDynamic Memory AllocationMemory Allocation FunctionsReleasing Memory (free)

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

Page 22: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

22

FIGURE 10-11 Memory Allocation

Page 23: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

23

FIGURE 10-13 Accessing Dynamic Memory

Page 24: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

24

FIGURE 10-14 Memory Management Functions

Page 25: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

Dynamic Memory Allocation

The malloc() and calloc() functions can frequently be used interchangeably The advantage of calloc() is that it initializes

all newly allocated numeric memory to 0 and character allocated memory to NULL

We use malloc() because it is the more general purpose of the two functions

malloc(10*sizeof(char)) or calloc(10,sizeof(char)) requests enough memory to store 10 character

Page 26: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

Necessary because malloc() returns void

Dynamic Memory Allocation (continued)

Page 27: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

27

Memory Allocation CastingPrior to C99, it was necessary to cast the pointer returned from a memory allocation function. While it is no longer necessary, it does no harm as long as the cast is correct.If you should be working with an earlier standard, the

casting format is: pointer = (type*) malloc(size)

NoteNote

Page 28: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

28

FIGURE 10-15 malloc

pointer = (type*) malloc(size)

Page 29: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

29

FIGURE 10-16 calloc

Page 30: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

30

FIGURE 10-17 realloc

Page 31: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

31

FIGURE 10-18 Freeing Memory

Page 32: 1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write

32

Using a pointer after its memory has been released is a common programming error. Guard against it

by clearing the pointer.

NoteNote

The pointer used to free memory must be of the same type as the pointer used to allocate the memory.

NoteNote


Top Related