BFM/BHM 2013 Programming For Engineers: Pointers and Array
BFM/BHM 2013 Programming For Engineers: Pointers and Array
Week 4
Faculty of Manufacturing
Universiti Malaysia Pahang Kampus Pekan, Pahang Darul Makmur Tel: +609-424 5800 Fax: +609-4245888
Program 9-5
// This program shows an array name being dereferenced // with the * operator.
#include <iostream.h>
void main(void) { short numbers[] = {10, 20, 30, 40, 50}; cout << "The first element of the array is "; cout << *numbers << endl; }
Program Output
The first element in the array is 10
Pointers in Expressions
Given:
int vals[]={4,7,11}; int *valptr = vals;
What is valptr + 1?
It means (address in valptr) + (1 * size of an int) cout << *(valptr+1); // displays 7 cout << *(valptr+2); // displays 11
Array Access
Array elements can be accessed in many ways
Array access method
array name and [ ]
pointer to array and [ ] valptr[2] = 17; array name and subscript arithmetic pointer to array and subscript arithmetic
10-6
Array Access
Array notation
vals[i] is equivalent to the pointer notation *(vals + i) No bounds checking performed on array access
10-7 PFE / SEM2 2013/2014
Figure 9-3
numbers[0]
numbers[1]
numbers[2]
numbers[3]
numbers[4]
numbers
Figure 9-4
numbers[0]
numbers[1]
numbers[2]
numbers[3]
numbers[4]
numbers
(numbers+1)
(numbers+2)
(numbers+3)
(numbers+4)
Program 9-6
// This program processes the contents of an array. Pointer // notation is used. #include <iostream.h>
10
11
Program 9-7
// This program uses subscript notation with a pointer and // pointer notation with an array name. #include <iostream.h> void main(void) { float coins[5] = {0.05, 0.1, 0.25, 0.5, 1.0}; float *floatPtr; // Pointer to a float int count; // array index floatPtr = coins; // floatPtr now points to coins array cout.precision(2); cout << "Here are the values in the coins array:\n";
12
Program continues
for (count = 0; count < 5; count++) cout << floatPtr[count] << " "; cout << "\nAnd here they are again:\n"; for (count = 0; count < 5; count++) cout << *(coins + count) << " "; cout << endl;
13
Program Output
Here are the values in the coins array: 0.05 0.1 0.25 0.5 1 And here they are again: 0.05 0.1 0.25 0.5 1
14
Program 9-8
// This program uses the address of each element in the array. #include <iostream.h> #include <iomanip.h> void main(void) { float coins[5] = {0.05, 0.1, 0.25, 0.5, 1.0}; float *floatPtr; // Pointer to a float int count; // array index cout.precision(2); cout << "Here are the values in the coins array:\n";
15
Program continues
for (count = 0; count < 5; count++) { floatPtr = &coins[count]; cout << *floatPtr << " "; } cout << endl;
16
Program Output
Here are the values in the coins array: 0.05 0.1 0.25 0.5 1
17
18
Pointer Arithmetic
Assume the variable definitions int vals[]={4,7,11}; int *valptr = vals; Examples of use of ++ and -valptr++; // points at 7 valptr--; // now points at 4
10-19
10-20
10-21
Program 9-9
// This program uses a pointer to display the contents // of an integer array. #include <iostream.h> void main(void) { int set[8] = {5, 10, 15, 20, 25, 30, 35, 40}; int *nums, index; nums = set; cout << "The numbers in set are:\n"; for (index = 0; index < 8; index++) { cout << *nums << " "; nums++; }
23
Program continues
cout << "\nThe numbers in set backwards are:\n"; for (index = 0; index < 8; index++) { nums--; cout << *nums << " "; }
}
24
Program Output
The numbers in set are: 5 10 15 20 25 30 35 40 The numbers in set backwards are: 40 35 30 25 20 15 10 5
25
27
10-28
Figure 9-5
An array of five integers array[0] array[1] array[2] array[3] array[4]
0x5A00
(Addresses)
0x5A04
0x5A08
0x5A0C
0x5A0F
29
Program 9-10
// This program uses a pointer to display the contents // of an integer array. #include <iostream.h>
void main(void) { int set[8] = {5, 10, 15, 20, 25, 30, 35, 40}; int *nums = set; // Make nums point to set
cout << "The numbers in set are:\n"; cout << *nums << " "; // Display first element while (nums < &set[7]) { nums++; cout << *nums << " "; }
30
Program continues
cout << "\nThe numbers in set backwards are:\n"; cout << *nums << " "; // Display last element while (nums > set) { nums--; cout << *nums << " "; }
31
Program Output
The numbers in set are: 5 10 15 20 25 30 35 40 The numbers in set backwards are: 40 35 30 25 20 15 10 5
32
33
void getNum(int *ptr); 2) asterisk * in body to dereference the pointer cin >> *ptr; 3) address as argument to the function getNum(&num);
10-35 PFE / SEM2 2013/2014
Program 9-11
// This program uses two functions that accept addresses of // variables as arguments. #include <iostream.h>
37
Program continues
// Definition of getNumber. The parameter, Input, is a pointer. // This function asks the user for a number. The value entered // is stored in the variable pointed to by Input.
void getNumber(int *input) { cout << "Enter an integer number: "; cin >> *input; }
// Definition of doubleValue. The parameter, val, is a pointer. // This function multiplies the variable pointed to by val by // two. void doubleValue(int *val) { *val *= 2; }
38
39
Program 9-12
// This program demonstrates that a pointer may be used as a // parameter to accept the address of an array. Either subscript // or pointer notation may be used. #include <iostream.h> #include <iomanip.h> // Function prototypes void getSales(float *); float totalSales(float *); void main(void) { float sales[4]; getSales(sales); cout.precision(2);
40
Program continues
cout.setf(ios::fixed | ios::showpoint); cout << "The total sales for the year are $"; cout << totalSales(sales) << endl; }
// // // // //
Definition of getSales. This function uses a pointer to accept the address of an array of four floats. The function asks the user to enter the sales figures for four quarters, and stores those figures in the array. (The function uses subscript notation.)
void getSales(float *array) { for (int count = 0; count < 4; count++) { cout << "Enter the sales figure for quarter "; cout << (count + 1) << ": "; cin >> array[count]; } }
41
Program continues
// // // // Definition of totalSales. This function uses a pointer to accept the address of an array of four floats. The function gets the total of the elements in the array and returns that value. (Pointer notation is used in this function.)
float totalSales(float *array) { float sum = 0.0; for (int count = 0; count < 4; count++) { sum += *array; array++; } return sum; }
42
43
10-44
Ponters to Constant
Must use const keyword in pointer definition:
const double taxRates[] = {0.65, 0.8, 0.75}; const double *ratePtr;
Use const keyword for pointers in function headers to protect data from modification from within function
10-45 PFE / SEM2 2013/2014
10-46
Constant Pointers
Defined with const keyword adjacent to variable name:
int classSize = 24; int * const classPtr = &classSize;
Must be initialized when defined Can be used without initialization as a function parameter
Initialized by argument when function is called Function can receive different arguments on different calls
While the address in the pointer cannot change, the data at that address may be changed
10-47 PFE / SEM2 2013/2014
10-48