Академический Документы
Профессиональный Документы
Культура Документы
Second Edition
Chapter 12 Pointers
Objectives
Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming Errors
To declare a pointer, specify the type of the variable to which it will point
Syntax: dataType *pointerName; Example: int *numAddr;
C++ for Engineers and Scientists, Second Edition 5
10
Compiler automatically performs an indirect access of bs value without explicitly using indirection symbol *; called automatic dereference
C++ for Engineers and Scientists, Second Edition 12
This example uses explicit dereference Pointer can be changed to point to another variable Indirection operator * must be used; also called dereferencing operator
C++ for Engineers and Scientists, Second Edition 13
15
16
17
18
19
Array declared with size causes static allocation of memory for all the elements, whether used or not
20
22
Pointer Arithmetic
Arithmetic operations can be done on pointer variables (as long as a valid address results) For arrays, the compiler will scale the arithmetic operation to ensure that the result points to a value of the correct type Addresses can be incremented or decremented using prefix and postfix increment and decrement operators
23
24
25
26
Passing Addresses
Addresses can be implicitly passed to functions using reference variables Addresses can be explicitly passed to functions using pointers; this is a pass by reference Place the address operator & in front of the variable to pass its address
Example: swap(&firstnum, &secnum)
27
When an array is passed to a function, only its address is actually passed (an entire array is always passed by reference) The address of the array is also the address of element 0
28
29
30
31
32
33
Summary
Address of a variable can be obtained using the address operator & Pointer variable stores the address of another variable Array name is a pointer constant Access to an array element using subscript notation can be replaced using pointer notation Arrays can be dynamically created at run time
36
Summary (continued)
Arrays are passed to functions as addresses When passing a single-dimensional array to a function, the function argument can be declared as a pointer or as an array Pointers can be incremented, decremented, compared, and assigned Values added to or subtracted from a pointer are automatically scaled based on the data type
37
LABORATORY EXERCISE
SAVE IN FOLDER : LabEx2-Yname FILE NAME : LabEx3Pointer Make a program that enters the ffg choices: [1]miles-gallons-mpg Write a program that declares three single-dimensional arrays named miles, gallons, and mpg. Each array should be capable of holding ten elements. In the miles array store the numbers 240.5, 300.0, 189.6, 310.6, 280.7, 216.9, 199.4, 160.3, 177.4, 192.3. In the gallons array store the numbers 10.3, 15.6, 8.7,14,16.3,15.7, 14.9, 10.7, 8.3, 8.4. Each element of the mpg array should be calculated as the corresponding element of the mi1esarray divided by the equivalent element of the gallons array; for example, mpg [0 ] =miles [0] / gallons [0] . Use pointers to calculate and display the elements of the mpg array.
LABORATORY EXERCISE
SAVE IN FOLDER : LabEx2-Yname FILE NAME : LabEx3Pointer Make a program that enters the ffg choices: [2]month-year Create a program that would input year and month in number. Display the year and the number of days in that month. Note* you must include the leap year e.g. if the input year is 2000 and the input month is 2 (February) the output no of days must be 29 days. (use function, arrays and pointers) Sample Output:
***A leap year is divisible by 4 but not by 100 (except if divisible by 400).