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
Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot be changed. Since the array name is a pointer constant to the first element, the address of the first element and the name of the array both represent the same location in memory.
3 FIGURE 10-1 Pointers to Arrays
4 same a &a[0] a is a pointer only to the first element—not the whole array. Note The name of an array is a pointer constant; it cannot be used as an lvalue. Note
5 FIGURE 10-2 Dereference of Array Name
6 FIGURE 10-3 Array Names as Pointers
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. Note 4
Pointer Arithmetic and Arrays Besides indexing, programmers use another powerful method of moving through an array: pointer arithmetic. Pointer arithmetic offers a restricted set of arithmetic operators for manipulating the addresses in pointers. Pointers and One-Dimensional Arrays Arithmetic Operations on Pointers Using Pointer Arithmetic Pointers and Two-Dimensional Arrays Topics discussed in this section:
9 FIGURE 10-5 Pointer Arithmetic Given pointer, p, p ± n is a pointer to the value n elements away. Note
10 FIGURE 10-6 Pointer Arithmetic and Different Types a + n * (sizeof (one element)) a + n Note
11 FIGURE 10-7 Dereferencing Array Pointers The following expressions are identical. *(a + n) and a[n] Note
12 Table 10-1Pointers and Relational Operators
13 FIGURE 10-8 (Part I) Find Smallest
14 FIGURE 10-8 (Part II) Find Smallest
15 PROGRAM 10-1 Print Array with Pointers
16 FIGURE 10-9 Pointers to Two-dimensional Arrays
Passing an Array to a Function Now that we have discovered that the name of an array is actually a pointer to the first element, we can send the array name to a function for processing. When we pass the array, we do not use the address operator. Remember, the array name is a pointer constant, so the name is already the address of the first element in the array.
18 FIGURE Variables for Multiply Array Elements By 2
19 PROGRAM 10-3 Multiply Array Elements by 2
20 PROGRAM 10-3 Multiply Array Elements by 2
Memory Allocation Functions C gives us two choices when we want to reserve memory locations for an object: static allocation and dynamic allocation. Memory Usage Static Memory Allocation Dynamic Memory Allocation Memory Allocation Functions Releasing Memory (free) Topics discussed in this section:
22 FIGURE Memory Allocation
23 FIGURE Accessing Dynamic Memory
24 FIGURE Memory Management Functions
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
… Necessary because malloc() returns void Dynamic Memory Allocation (continued)
27 Memory Allocation Casting Prior 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) Note
28 FIGURE malloc pointer = (type*) malloc(size)
29 FIGURE calloc
30 FIGURE realloc
31 FIGURE Freeing Memory
32 Using a pointer after its memory has been released is a common programming error. Guard against it by clearing the pointer. Note The pointer used to free memory must be of the same type as the pointer used to allocate the memory. Note