// you’re reading...

C++ Glossary

Functions Using Array Ranges

C++ functions that process arrays need to be informed about

  • The kind of data in the array
  • The location of the beginning of the array
  • And the number of elements in the array



The traditional C/C++ approach to functions that process arrays

  • Is to pass a pointer to the start of the array as one argument
    • And to pass the size of the array as a second argument
      • (The pointer tells the function both where to find the array and the kind of data in it.)
        • That gives the function the information it needs to find all the data



There is another approach to giving a function the information it needs: to specify a range of elements

  • This can be done by passing two pointers
    • One identifying the start of the array and one identifying the end of the array
  • The C++ Standard Template Library, for example, generalizes the range approach
    • The STL approach uses the concept of “one past the end” to indicate an extent
      • That is, in the case of an array, the argument identifying the end of the array would be a pointer to the location just after the last element
    • For example, suppose you have this declaration (A-1):
      • Then the two pointers elboud and elboud + 20 define the range
        • First, elboud, being the name of the array, points to the first element
          • The expression elboud + 19 points to the last element (that is, elboud[19]), so elboud + 20 points to one past the end of the array
  • Passing a range to a function tells it
    • Which elements to process
  • (A-2) use two pointers to specify a range



Example of (A-1)

double elbuod[20];



Example of (A-2)

// arrfun4.cpp -- functions with an array range
#include <iostream>
const int ArSize = 8;
int sum_arr(const int * begin, const int * end);
int main()
{
	using namespace std;
	int cookies[ArSize] = {1,2,4,8,16,32,64,128};
// some systems require preceding int with static to
// enable array initialization
 
	int sum = sum_arr(cookies, cookies + ArSize);
	cout << "Total cookies eaten: " << sum << endl;
	sum = sum_arr(cookies, cookies + 3); // first 3 elements
	cout << "First three eaters ate " << sum << " cookies.\n";
	sum = sum_arr(cookies + 4, cookies + 8); // last 4 elements
	cout << "Last four eaters ate " << sum << " cookies.\n";
	return 0;
}
 
// return the sum of an integer array
int sum_arr(const int * begin, const int * end)
{
	const int * pt;
	int total = 0;
	for (pt = begin; pt != end; pt++)
		total = total + *pt;
	return total;
}



Here’s the output of the program in (A-2):

Output




Program Notes



In (A-2), notice the for loop in the sum_array() function (A-3)

  • It sets pt to point to the first element to be processed (the one pointed to by begin) and adds *pt (the value of the element) to total
    • Then the loop updates pt by incrementing it, causing it to point to the next element
      • The process continues as long as pt != end
        • When pt finally equals end, it’s pointing to the location following the last element of the range, so the loop halts



Example of (A-3)

for (pt = begin; pt != end; pt++)
	total = total + *pt;



Second, notice how the different function calls specify different ranges within the array (A-4)

  • The pointer value cookies + ArSize points to the location following the last element
    • (The array has ArSize elements, so cookies[ArSize - 1] is the last element, and its address is cookies + ArSize – 1.)
      • So the range cookies, cookies + ArSize specifies the entire array
        • Similarly, cookies, cookies + 3 specifies the first three elements, and so on
  • Note, by the way, that the rules for pointer subtraction imply that
    • In sum_arr(), the expression end - begin is an integer value equal to the number of elements in the range



Example of (A-4)

int sum = sum_arr(cookies, cookies + ArSize);
...
sum = sum_arr(cookies, cookies + 3); // first 3 elements
...
sum = sum_arr(cookies + 4, cookies + 8); // last 4 elements


Discussion

No comments for “Functions Using Array Ranges”

Post a comment