<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C ++ &#187; Pointer</title>
	<atom:link href="http://www.bizzymicbizness.com/cplusplus/tag/pointer/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bizzymicbizness.com/cplusplus</link>
	<description>All about C++</description>
	<lastBuildDate>Mon, 19 Oct 2009 12:38:17 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Functions Using Array Ranges</title>
		<link>http://www.bizzymicbizness.com/cplusplus/functions-using-array-ranges</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/functions-using-array-ranges#comments</comments>
		<pubDate>Mon, 19 Oct 2009 06:34:39 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[Range]]></category>
		<category><![CDATA[STL]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=836</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>C++ functions that process arrays need to be informed about</p>
<ul>
<li>The kind of data in the array</li>
<li>The location of the beginning of the array</li>
<li>And the number of elements in the array</li>
</ul>
<p><br class="blank" /><br />
The traditional C/C++ approach to functions that process arrays</p>
<ul>
<li>Is to pass a pointer to the start of the array as one argument
<ul>
<li>And to pass the size of the array as a second argument
<ul>
<li>(The pointer tells the function both where to find the array and the kind of data in it.)
<ul>
<li>That gives the function the information it needs to find all the data</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
There is another approach to giving a function the information it needs: to specify a <em>range</em> of elements</p>
<ul>
<li>This can be done by passing two pointers</li>
<ul>
<li>One identifying the start of the array and one identifying the end of the array</li>
</ul>
<li>The C++ Standard Template Library, for example, generalizes the range approach</li>
<ul>
<li>The STL approach uses the concept of “one past the end” to indicate an extent</li>
<ul>
<li>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</li>
</ul>
<li>For example, suppose you have this declaration (A-1):</li>
<ul>
<li>Then the two pointers <strong>elboud</strong> and <strong>elboud + 20</strong> define the range</li>
<ul>
<li>First, <strong>elboud</strong>, being the name of the array, points to the first element</li>
<ul>
<li>The expression <strong>elboud + 19</strong> points to the last element (that is, <strong>elboud[19]</strong>), so <strong>elboud + 20</strong> points to one past the end of the array</li>
</ul>
</ul>
</ul>
</ul>
<li>Passing a range to a function tells it</li>
<ul>
<li>Which elements to process</li>
</ul>
<li>(A-2) use two pointers to specify a range</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">double</span> elbuod<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// arrfun4.cpp -- functions with an array range</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> ArSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> begin, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> end<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> cookies<span style="color: #008000;">&#91;</span>ArSize<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">2</span>,<span style="color: #0000dd;">4</span>,<span style="color: #0000dd;">8</span>,<span style="color: #0000dd;">16</span>,<span style="color: #0000dd;">32</span>,<span style="color: #0000dd;">64</span>,<span style="color: #0000dd;">128</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// some systems require preceding int with static to</span>
<span style="color: #666666;">// enable array initialization</span>
&nbsp;
	<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, cookies <span style="color: #000040;">+</span> ArSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Total cookies eaten: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// first 3 elements</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;First three eaters ate &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; cookies.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">4</span>, cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">8</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// last 4 elements</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Last four eaters ate &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; cookies.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// return the sum of an integer array</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> begin, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> end<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> pt<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>pt <span style="color: #000080;">=</span> begin<span style="color: #008080;">;</span> pt <span style="color: #000040;">!</span><span style="color: #000080;">=</span> end<span style="color: #008080;">;</span> pt<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		total <span style="color: #000080;">=</span> total <span style="color: #000040;">+</span> <span style="color: #000040;">*</span>pt<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here’s the output of the program in (A-2):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-837" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output7.jpg" alt="Output" width="597" height="275" /></p>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Program Notes</h2>
<p><br class="blank" /><br />
In (A-2), notice the <strong>for</strong> loop in the <strong>sum_array()</strong> function (A-3)</p>
<ul>
<li>It sets <strong>pt</strong> to point to the first element to be processed (the one pointed to by <strong>begin</strong>) and adds <strong>*pt</strong> (the value of the element) to <strong>total</strong>
<ul>
<li>Then the loop updates <strong>pt</strong> by incrementing it, causing it to point to the next element
<ul>
<li>The process continues as long as <strong>pt != end</strong>
<ul>
<li>When <strong>pt</strong> finally equals <strong>end</strong>, it’s pointing to the location following the last element of the range, so the loop halts</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>pt <span style="color: #000080;">=</span> begin<span style="color: #008080;">;</span> pt <span style="color: #000040;">!</span><span style="color: #000080;">=</span> end<span style="color: #008080;">;</span> pt<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	total <span style="color: #000080;">=</span> total <span style="color: #000040;">+</span> <span style="color: #000040;">*</span>pt<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Second, notice how the different function calls specify different ranges within the array (A-4)</p>
<ul>
<li>The pointer value <strong>cookies + ArSize</strong> points to the location following the last element</li>
<ul>
<li>(The array has <strong>ArSize</strong> elements, so <strong>cookies[ArSize - 1]</strong> is the last element, and its address is <strong>cookies + ArSize &#8211; 1</strong>.)</li>
<ul>
<li>So the range <strong>cookies</strong>, <strong>cookies + ArSize</strong> specifies the entire array</li>
<ul>
<li>Similarly, <strong>cookies</strong>, <strong>cookies + 3</strong> specifies the first three elements, and so on</li>
</ul>
</ul>
</ul>
<li>Note, by the way, that the rules for pointer subtraction imply that</li>
<ul>
<li>In <strong>sum_arr()</strong>, the expression <strong>end</strong> <strong>-</strong> <strong>begin</strong> is an integer value equal to the number of elements in the range</li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, cookies <span style="color: #000040;">+</span> ArSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
...
<span style="color: #007788;">sum</span> <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// first 3 elements</span>
...
<span style="color: #007788;">sum</span> <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">4</span>, cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">8</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// last 4 elements</span></pre></div></div>

<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/functions-using-array-ranges/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Array Function Examples</title>
		<link>http://www.bizzymicbizness.com/cplusplus/more-array-function-examples</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/more-array-function-examples#comments</comments>
		<pubDate>Sun, 18 Oct 2009 12:36:59 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Borland C++]]></category>
		<category><![CDATA[Bottom-Up Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[const]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[iostream]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[Procedural Programming]]></category>
		<category><![CDATA[Top-Down Programming]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=801</guid>
		<description><![CDATA[When you choose to use an array to represent data, you are making a design decision

But design decisions should go beyond how data is stored; they should also involve how the data is used


Often, you’ll find it profitable to write specific functions to handle specific data operations

(The profits here include increased program reliability, ease of [...]]]></description>
			<content:encoded><![CDATA[<p>When you choose to use an array to represent data, you are making a design decision</p>
<ul>
<li>But design decisions should go beyond how data is stored; they should also involve how the data is used</li>
</ul>
<p><br class="blank" /><br />
Often, you’ll find it profitable to write specific functions to handle specific data operations</p>
<ul>
<li>(The profits here include increased program reliability, ease of modification, and ease of debugging.)
<ul>
<li>Also, when you begin integrating storage properties with operations when you think about a program, you are taking an important step toward the OOP mind-set; that, too, might prove profitable in the future</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Let’s examine a simple case</p>
<ul>
<li>Suppose you want to use an array to keep track of the dollar values of your real estate
<ul>
<li>(If necessary, suppose you have real estate.)
<ul>
<li>You have to decide what type to use
<ul>
<li>Certainly, <strong>double</strong> is less restrictive in its range than <strong>int</strong> or <strong>long</strong>, and it provides enough significant digits to represent the values precisely</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Next, you have to decide on the number of array elements</p>
<ul>
<li>(With dynamic arrays created with new, you can put off that decision, but let’s keep things simple.)
<ul>
<li>Let’s say that you have no more than five properties, so you can use an array of five <strong>doubles</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Now consider the possible operations you might want to execute with the real estate array</p>
<ul>
<li>Two very basic ones are:</li>
<ul>
<li>Reading values into the array</li>
<li>And displaying the array contents</li>
</ul>
<li>Let’s add one more operation to the list:</li>
<ul>
<li>Reassessing the value of the properties</li>
<ul>
<li>For simplicity, assume that all your properties increase or decrease in value at the same rate</li>
</ul>
</ul>
<li>(Remember, this is a topic in C++, not on real estate management.)</li>
</ul>
<p><br class="blank" /><br />
Next, fit a function to each operation and then write the code accordingly</p>
<ul>
<li>We’ll go through the steps of creating these pieces of a program next
<ul>
<li>Afterward, we’ll fit them into a complete example</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Filling the Array</h2>
<p><br class="blank" /><br />
Because a function with an array name argument accesses the original array, not a copy, you can use a function call to assign values to array elements</p>
<ul>
<li>One argument to the function will be the name of the array to be filled
<ul>
<li>In general, a program might manage more than one person’s investments, hence more than one array, so you don’t want to build the array size into the function</li>
<ul>
<li>Instead, you pass the array size as a second argument</li>
</ul>
</ul>
<li>Also, it’s possible that you might want to quit reading data before filling the array, so you want to build that feature in to the function</li>
<ul>
<li>Because you might enter fewer than the maximum number of elements, it makes sense to have the function return the actual number of values entered
<ul>
<li>These considerations suggest the following function prototype (A-1)</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
The function takes an array name argument and an argument specifying the maximum number of items to be read, and the function returns the actual number of items read</p>
<ul>
<li>For example, if you use this function with an array of five elements, you pass <strong>5</strong> as the second argument
<ul>
<li>If you then enter only three values, the function returns <strong>3</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
You can use a loop to read successive values into the array, but how can you terminate the loop early?</p>
<ul>
<li>One way is to use a special value to indicate the end of input</li>
<ul>
<li>Because no property should have a negative value, you can use a negative number to indicate the end of input</li>
</ul>
<li>Also, the function should do something about bad input</li>
<ul>
<li>Such as terminating further input</li>
</ul>
<li>Given this, you can code the function as follows (A-2):</li>
<ul>
<li>Note that this code includes a prompt to the user</li>
<ul>
<li>If the user enters a non-negative value, the value is assigned to the array</li>
<ul>
<li>Otherwise, the loop terminates</li>
</ul>
<li>If the user enters only valid values</li>
<ul>
<li>The loop terminates after it reads limit values</li>
</ul>
</ul>
<li>The last thing the loop does is increment <strong>i</strong>, so after the loop terminates, <strong>i</strong> is one greater than the last array index, hence it’s equal to the number of filled elements</li>
<ul>
<li>The function then returns that value</li>
</ul>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> temp<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> limit<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter value #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> temp<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span><span style="color: #0000dd;">cin</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// bad input</span>
		<span style="color: #008000;">&#123;</span>
&nbsp;
			<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span>
				<span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bad input; input process terminated.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>temp <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// signal to terminate</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> temp<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> i<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Showing the Array and Protecting It with const</h2>
<p><br class="blank" /><br />
Building a function to display the array contents is simple</p>
<ul>
<li>You pass the name of the array and the number of filled elements to the function, which then uses a loop to display each element</li>
</ul>
<p><br class="blank" /><br />
But there is another consideration—guaranteeing that the display function doesn’t alter the original array</p>
<ul>
<li>Unless the purpose of a function is to alter data passed to it, you should safeguard it from doing so</li>
<ul>
<li>That protection comes automatically with ordinary arguments because C++ passes them by value, and the function works with a copy</li>
</ul>
<li>But functions that use an array work with the original</li>
<ul>
<li>After all, that’s why the <strong>fill_array()</strong> function is able to do its job</li>
</ul>
</ul>
<p><br class="blank" /><br />
To keep a function from accidentally altering the contents of an array argument</p>
<ul>
<li>You can use the keyword <strong>const</strong> when you declare the formal argument (A-3)</li>
<ul>
<li>The declaration states that the pointer <strong>ar</strong> points to constant data</li>
<ul>
<li>This means that you can’t use <strong>ar</strong> to change the data</li>
<ul>
<li>That is, you can use a value such as <strong>ar[0]</strong>, but you can’t change that value</li>
</ul>
</ul>
</ul>
<li>Note that this doesn’t mean that the original array needs be constant; it just means that you can’t use <strong>ar</strong> in the <strong>show_array()</strong> function to change the data</li>
<ul>
<li>Thus, <strong>show_array()</strong> treats the array as read-only data</li>
</ul>
<li>Suppose you accidentally violate this restriction by doing something like the following in the <strong>show_array()</strong> function (A-4)</li>
<ul>
<li>In this case, the compiler will put a stop to your wrongful ways</li>
<ul>
<li>Borland C++, for example, gives an error message like this (edited slightly) (A-5)</li>
<ul>
<li>Other compilers may choose to express their displeasure in different words</li>
</ul>
<li>The message reminds you that C++ interprets the declaration <strong>const double</strong> <strong>ar[]</strong> to mean <strong>const double *ar</strong></li>
<ul>
<li>Thus, the declaration really says that <strong>ar</strong> points to a constant value</li>
<ul>
<li>We’ll discuss this in detail when we finish with the current example</li>
</ul>
</ul>
</ul>
</ul>
<li>Meanwhile, here is the code for the <strong>show_array()</strong> function (A-6)</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">ar<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-5)</p>
<div class="syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">
Cannot modify a const object in function show_array(const double *,int)
</pre>
</div>
</div>
<p><br class="blank" /><br />
Example of (A-6)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Property #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: $&quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Modifying the Array</h2>
<p><br class="blank" /><br />
The third operation for the array in this example is multiplying each element by the same revaluation factor</p>
<ul>
<li>You need to pass three arguments to the function:</li>
<ul>
<li>The factor</li>
<li>The array</li>
<li>And the number of elements</li>
</ul>
<li>No return value is needed, so the function can look like this (A-7)</li>
<ul>
<li>Because this function is supposed to alter the array values, you don’t use <strong>const</strong> when you declare <strong>ar</strong></li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-7)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> revalue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> r<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Putting the Pieces Together</h2>
<p><br class="blank" /><br />
Now that you’ve defined a data type in terms of how it’s stored (an array) and how it’s used (three functions), you can put together a program that uses the design</p>
<ul>
<li>Because you’ve already built all the array-handling tools</li>
<ul>
<li>You’ve greatly simplified programming <strong>main()</strong></li>
</ul>
<li>Most of the remaining programming work consists of having <strong>main()</strong> call the functions you’ve just developed</li>
<li>(A-8) shows the result</li>
<ul>
<li>It places a <strong>using</strong> directive in just those functions that use the <strong>iostream</strong> facilities</li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-8)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// arrfun3.cpp -- array functions and const</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> Max <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// function prototypes</span>
<span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// don’t change data</span>
<span style="color: #0000ff;">void</span> revalue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> properties<span style="color: #008000;">&#91;</span>Max<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">int</span> size <span style="color: #000080;">=</span> fill_array<span style="color: #008000;">&#40;</span>properties, Max<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	show_array<span style="color: #008000;">&#40;</span>properties, size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter reassessment rate: &quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> factor<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> factor<span style="color: #008080;">;</span>
	revalue<span style="color: #008000;">&#40;</span>factor, properties, size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	show_array<span style="color: #008000;">&#40;</span>properties, size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Done.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> temp<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> limit<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter value #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> temp<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span><span style="color: #0000dd;">cin</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// bad input</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span>
				<span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bad input; input process terminated.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>temp <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// signal to terminate</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> temp<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> i<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// the following function can use, but not alter,</span>
<span style="color: #666666;">// the array whose address is ar</span>
<span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Property #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: $&quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// multiplies each element of ar[] by r</span>
<span style="color: #0000ff;">void</span> revalue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> r<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here are two sample runs of the program in (A-8):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-802" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output6.jpg" alt="Output" width="598" height="275" /></p>
<p><br class="blank" /></p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-803" title="Output Second" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output-Second2.jpg" alt="Output Second" width="597" height="275" /></p>
<p><br class="blank" /><br />
Recall that <strong>fill_array()</strong> prescribes that input should quit when the user enters five properties or enters a negative number, whichever comes first</p>
<ul>
<li>The first output example illustrates reaching the five-property limit</li>
<ul>
<li>And the second output example illustrates that entering a negative value terminates the input phase</li>
</ul>
</ul>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Program Notes</h2>
<p><br class="blank" /><br />
We’ve already discussed the important programming details related to the real estate example, so let’s reflect on the process</p>
<ul>
<li>You began by thinking about the data type and designed appropriate functions to handle the data</li>
<ul>
<li>Then, you assembled these functions into a program</li>
<ul>
<li>This is sometimes called bottom-up programming because the design process moves from the component parts to the whole</li>
<ul>
<li>This approach is well suited to OOP, which concentrates on data representation and manipulation first</li>
</ul>
</ul>
</ul>
<li>Traditional procedural programming, on the other hand, leans toward top-down programming</li>
<ul>
<li>In which you develop a modular grand design first and then turn your attention to the details</li>
</ul>
<li>Both methods are useful, and both lead to modular programs</li>
</ul>
<p><br class="blank" /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/more-array-function-examples/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Implications of Using Arrays as Arguments</title>
		<link>http://www.bizzymicbizness.com/cplusplus/the-implications-of-using-arrays-as-arguments</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/the-implications-of-using-arrays-as-arguments#comments</comments>
		<pubDate>Sat, 17 Oct 2009 09:02:15 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Function Call]]></category>
		<category><![CDATA[Hexadecimal]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[sizeof]]></category>
		<category><![CDATA[std::]]></category>
		<category><![CDATA[Variable]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=789</guid>
		<description><![CDATA[Let’s look at the implications of (A-1)

The function call sum_arr(cookies, ArSize)

Passes the address of the first element of the cookies array and the number of elements of the array to the sum_arr() function

The sum_arr() function

Assigns the cookies address to the pointer variable arr and assigns ArSize to the int variable n

This means (A-1) doesn’t really [...]]]></description>
			<content:encoded><![CDATA[<p>Let’s look at the implications of (A-1)</p>
<ul>
<li>The function call <strong>sum_arr(cookies, ArSize)</strong></li>
<ul>
<li>Passes the address of the first element of the <strong>cookies</strong> array and the number of elements of the array to the <strong>sum_arr() </strong>function</li>
</ul>
<li>The <strong>sum_arr()</strong> function</li>
<ul>
<li>Assigns the <strong>cookies</strong> address to the pointer variable <strong>arr</strong> and assigns <strong>ArSize</strong> to the <strong>int</strong> variable <strong>n</strong></li>
</ul>
<li>This means (A-1) doesn’t really pass the array contents to the function</li>
<ul>
<li>Instead, it tells the function where the array is (the address), what kind of elements it has (the type), and how many elements it has (the <strong>n</strong> variable)</li>
<ul>
<li>See (Image-1)</li>
</ul>
</ul>
<li>Armed with this information, the function then uses the original array</li>
<ul>
<li>If you pass an ordinary variable, the function works with a copy</li>
<ul>
<li>But if you pass an array, the function works with the original</li>
</ul>
</ul>
<li>Actually, this difference doesn’t violate C++’s pass-by-value approach</li>
<ul>
<li>The <strong>sum_arr()</strong> function still passes a value that’s assigned to a new variable</li>
<ul>
<li>But that value is a single address, not the contents of an array</li>
</ul>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// arrfun1.cpp -- functions with an array argument</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> ArSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// prototype</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> cookies<span style="color: #008000;">&#91;</span>ArSize<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">2</span>,<span style="color: #0000dd;">4</span>,<span style="color: #0000dd;">8</span>,<span style="color: #0000dd;">16</span>,<span style="color: #0000dd;">32</span>,<span style="color: #0000dd;">64</span>,<span style="color: #0000dd;">128</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// some systems require preceding int with static to</span>
<span style="color: #666666;">// enable array initialization</span>
&nbsp;
	<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, ArSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Total cookies eaten: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// return the sum of an integer array</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		total <span style="color: #000080;">=</span> total <span style="color: #000040;">+</span> arr<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
(Image-1) Telling a function about an array</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-791" title="Telling a function about an array" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Telling-a-function-about-an-array.jpg" alt="Telling a function about an array" width="498" height="306" /></p>
<p><br class="blank" /><br />
Is the correspondence between array names and pointers a good thing?</p>
<ul>
<li>Indeed, it is</li>
</ul>
<p><br class="blank" /><br />
The design decision to use array addresses as arguments saves the time and memory needed to copy an entire array</p>
<ul>
<li>The overhead for using copies can be prohibitive if you’re working with large arrays</li>
<ul>
<li>With copies, not only does a program need more computer memory, but it has to spend time copying large blocks of data</li>
</ul>
<li>On the other hand, working with the original data raises the possibility of inadvertent data corruption</li>
<ul>
<li>That’s a real problem in classic C, but ANSI C and C++’s <strong>const</strong> modifier provides a remedy</li>
<ul>
<li>You’ll soon see an example</li>
</ul>
</ul>
<li>But first, let’s alter (A-1) to illustrate some points about how array functions operate</li>
</ul>
<p><br class="blank" /><br />
(A-2) demonstrates that <strong>cookies</strong> and <strong>arr</strong> have the same value</p>
<ul>
<li>It also shows how the pointer concept makes the <strong>sum_arr</strong> function more versatile than it may have appeared at first
<ul>
<li>To provide a bit of variety and to show you what it looks like, the program uses the <strong>std::</strong> qualifier instead of the <strong>using</strong> directive to provide access to <strong>cout</strong> and <strong>endl</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// arrfun2.cpp -- functions with an array argument</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> ArSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// use std:: instead of using directive</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> cookies<span style="color: #008000;">&#91;</span>ArSize<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">2</span>,<span style="color: #0000dd;">4</span>,<span style="color: #0000dd;">8</span>,<span style="color: #0000dd;">16</span>,<span style="color: #0000dd;">32</span>,<span style="color: #0000dd;">64</span>,<span style="color: #0000dd;">128</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// some systems require preceding int with static to</span>
<span style="color: #666666;">// enable array initialization</span>
&nbsp;
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> cookies <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = array address, &quot;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// some systems require a type cast: unsigned (cookies)</span>
&nbsp;
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">sizeof</span> cookies <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = sizeof cookies<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, ArSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Total cookies eaten: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
	sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// a lie</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;First three eaters ate &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; cookies.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// another lie</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Last four eaters ate &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; cookies.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// return the sum of an integer array</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> arr <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = arr, &quot;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// some systems require a type cast: unsigned (arr)</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">sizeof</span> arr <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = sizeof arr<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		total <span style="color: #000080;">=</span> total <span style="color: #000040;">+</span> arr<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here’s the output of the program in (A-2): </p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-790" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output5.jpg" alt="Output" width="538" height="248" /></p>
<p><br class="blank" /><br />
Note that the address values and the array and integer sizes will vary from system to system</p>
<ul>
<li>Also, some implementations will display the addresses in base 10 notation instead of in hexadecimal</li>
</ul>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Program Notes</h2>
<p><br class="blank" /><br />
(A-2) illustrates some very interesting points about array functions</p>
<ul>
<li>First, note that <strong>cookies</strong> and <strong>arr</strong> both evaluate to the same address, exactly as claimed</li>
<li>But <strong>sizeof cookies</strong> is <strong>32</strong>, whereas <strong>sizeof arr</strong> is only <strong>4</strong></li>
<ul>
<li>That’s because <strong>sizeof cookies</strong> is the size of the whole array, whereas <strong>sizeof arr</strong> is the size of the pointer variable</li>
</ul>
<li>By the way, this is why you have to explicitly pass the size of the array rather than use <strong>sizeof arr</strong> in <strong>sum_arr()</strong></li>
<ul>
<li>Because the only way <strong>sum_arr()</strong> knows the number of elements in the array is through what you tell it with the second argument, you can lie to the function</li>
<ul>
<li>For example, the second time the program uses the function, it makes this call (A-3)</li>
<ul>
<li>By telling the function that <strong>cookies</strong> has just three elements, you get the function to calculate the sum of the first three elements</li>
</ul>
</ul>
</ul>
<ul>
<li>Why stop there? You can also lie about where the array starts (A-4)</li>
<ul>
<li>Because <strong>cookies</strong> acts as the address of the first element, <strong>cookies + 4</strong> acts as the address of the fifth element</li>
<ul>
<li>This statement sums the fifth, sixth, seventh, and eighth elements of the array</li>
</ul>
</ul>
</ul>
<li>Note in the output how the third call to the function assigns a different address to <strong>arr</strong> than the first two calls did</li>
<ul>
<li>And yes, you can use <strong>&amp;cookies[4]</strong> instead of <strong>cookies + 4</strong> as the argument; they both mean the same thing</li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Remember</p>
<ul>
<li>To indicate the kind of array and the number of elements to an array-processing function, you pass the information as two separate arguments (A-5)
<ul>
<li>Don’t try to pass the array size by using brackets notation (A-6)</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-5)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> fillArray<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// prototype</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-6)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> fillArray<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span>size<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// NO -- bad prototype</span></pre></div></div>

<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/the-implications-of-using-arrays-as-arguments/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a Reference Variable</title>
		<link>http://www.bizzymicbizness.com/cplusplus/creating-a-reference-variable</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/creating-a-reference-variable#comments</comments>
		<pubDate>Fri, 16 Oct 2009 08:27:50 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[Reference Variable]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=771</guid>
		<description><![CDATA[C and C++ use the &#38; symbol to indicate the address of a variable

C++ assigns an additional meaning to the &#38; symbol

Presses it into service for declaring references

For example, to make rodents an alternative name for the variable rats, you could do the following (A-1):






Example of (A-1)

int rats;
int &#38; rodents = rats; // makes rodents [...]]]></description>
			<content:encoded><![CDATA[<p>C and C++ use the <strong>&amp;</strong> symbol to indicate the address of a variable</p>
<ul>
<li>C++ assigns an additional meaning to the <strong>&amp;</strong> symbol
<ul>
<li>Presses it into service for declaring references
<ul>
<li>For example, to make <strong>rodents</strong> an alternative name for the variable <strong>rats</strong>, you could do the following (A-1):</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> rats<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span> <span style="color: #666666;">// makes rodents an alias for rats</span></pre></div></div>

<p><br class="blank" /><br />
In this context, <strong>&amp;</strong> is not the address operator</p>
<ul>
<li>Instead, it serves as part of the type identifier
<ul>
<li>Just as <strong>char *</strong> in a declaration means pointer-to-<strong>char</strong>,<strong> int &amp;</strong> means reference-to-<strong>int</strong></li>
</ul>
</li>
</ul>
<p> <br />
<br class="blank" /><br />
The reference declaration allows you to use <strong>rats</strong> and <strong>rodents</strong> interchangeably; both refer to the same value and the same memory location</p>
<ul>
<li>(A-2) illustrates the truth of this claim</li>
</ul>
<p> </p>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// firstref.cpp -- defining and using a reference</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> rats <span style="color: #000080;">=</span> <span style="color: #0000dd;">255</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span> <span style="color: #666666;">// rodents is a reference</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;rats = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	rodents<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;rats = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// some implementations require type casting the following</span>
	<span style="color: #666666;">// addresses to type unsigned</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;rats address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Note that the <strong>&amp;</strong> operator in the statement (A-3)</p>
<ul>
<li>Is not the address operator
<ul>
<li>But declares that <strong>rodents</strong> is of type <strong>int &amp;</strong>—that is, it is a reference to an <strong>int</strong> variable</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
But the <strong>&amp;</strong> operator in the statement (A-4)</p>
<ul>
<li>Is the address operator
<ul>
<li>With <strong>&amp;rodents</strong> representing the address of the variable to which <strong>rodents</strong> refers</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Here is the output of the program in (A-2): </p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-772" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output4.jpg" alt="Output" width="598" height="275" /></p>
<p><br class="blank" /><br />
As you can see both, <strong>rats</strong> and <strong>rodents</strong> have the same value and the same address</p>
<ul>
<li>(The address values and display format vary from system to system.)</li>
</ul>
<p> <br />
<br class="blank" /><br />
Incrementing <strong>rodents</strong> by one, affects both variables</p>
<ul>
<li>More precisely, the <strong>rodents++</strong> operation increments a single variable for which there are two names</li>
</ul>
<p><br class="blank" /><br />
Again, keep in mind that although this example shows you how a reference works</p>
<ul>
<li>It doesn’t represent the typical use for a reference, which is as a function parameter, particularly for structure and object arguments
<ul>
<li>We’ll look into these uses pretty soon</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
References tend to be a bit confusing at first to C veterans coming to C++</p>
<ul>
<li>Because they are tantalizingly reminiscent of pointers, yet somehow different
<ul>
<li>For example, you can create both a reference and a pointer to refer to <strong>rats </strong>(A-3)</li>
</ul>
<li>Then, you could</li>
<ul>
<li>Use the expressions <strong>rodents</strong> and <strong>*prats</strong> interchangeably with <strong>rats</strong>
<ul>
<li>And use the expressions <strong>&amp;rodents</strong> and <strong>prats</strong> interchangeably with <strong>&amp;rats</strong></li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> rats <span style="color: #000080;">=</span> <span style="color: #0000dd;">255</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span> <span style="color: #666666;">// rodents a reference</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> prats <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>rats<span style="color: #008080;">;</span>  <span style="color: #666666;">// prats a pointer</span></pre></div></div>

<p><br class="blank" /><br />
From this standpoint</p>
<ul>
<li>A reference looks a lot like a pointer in disguised notation in which the <strong>*</strong> dereferencing operator is understood implicitly
<ul>
<li>And, in fact, that’s more or less what a reference is</li>
</ul>
<li>But there are differences besides those of notation</li>
<ul>
<li>For one, it is necessary to initialize the reference when you declare it; you can’t declare the reference and then assign it a value later the way you can with a pointer (A-4):</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> rat<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodent<span style="color: #008080;">;</span>
rodent <span style="color: #000080;">=</span> rat<span style="color: #008080;">;</span> <span style="color: #666666;">// No, you can’t do this</span></pre></div></div>

<p><br class="blank" /><br />
Remember</p>
<ul>
<li>You should initialize a reference variable when you declare it</li>
</ul>
<p><br class="blank" /><br />
A reference is rather like a <strong>const</strong> pointer; you have to initialize it when you create it</p>
<ul>
<li>And once a reference pledges its allegiance to a particular variable, it sticks to its pledge
<ul>
<li>That is, (A-5)</li>
</ul>
<li>(A-5) is, in essence, a disguised notation for something like this (A-6)
<ul>
<li>Here, the reference <strong>rodents</strong> plays the same role as the expression <strong>*pr</strong></li>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-5)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-6)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> pr <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>rats<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
(A-7) shows what happens</p>
<ul>
<li>If you try to make a reference change allegiance from a <strong>rats</strong> variable to a <strong>bunnies</strong> variable</li>
</ul>
<p><br class="blank" /><br />
Example of (A-7)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// secondref.cpp -- defining and using a reference</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> rats <span style="color: #000080;">=</span> <span style="color: #0000dd;">256</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span> <span style="color: #666666;">// rodents is a reference</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;rats = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;rats address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">int</span> bunnies <span style="color: #000080;">=</span> <span style="color: #0000dd;">128</span><span style="color: #008080;">;</span>
	rodents <span style="color: #000080;">=</span> bunnies<span style="color: #008080;">;</span> <span style="color: #666666;">// can we change the reference?</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;bunnies = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> bunnies<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rats = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;bunnies address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>bunnies<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here’s the output of the program in (A-7):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-773" title="Output Second" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output-Second1.jpg" alt="Output Second" width="599" height="276" /></p>
<p><br class="blank" /><br />
Initially, <strong>rodents</strong> refers to <strong>rats</strong></p>
<ul>
<li>But then the program apparently attempts to make <strong>rodents</strong> a reference to <strong>bunnies </strong>(A-8):</li>
<ul>
<li>For a moment, it looks as if this attempt has succeeded because the value of <strong>rodents</strong> changes from <strong>256</strong> to <strong>128</strong></li>
<ul>
<li>But closer inspection reveals that <strong>rats</strong> also has changed to <strong>128</strong> and that <strong>rats</strong> and <strong>rodents</strong> still share the same address</li>
<ul>
<li>Which differs from the <strong>bunnies</strong> address</li>
</ul>
</ul>
</ul>
<li>Because <strong>rodents</strong> is an alias for <strong>rats</strong></li>
<ul>
<li>The assignment statement really means the same as the following (A-9)</li>
<ul>
<li>That is, it means “Assign the value of the <strong>bunnies</strong> variable to the <strong>rat</strong> variable.”</li>
</ul>
</ul>
<li>In short, you can set a reference by an initializing declaration</li>
<ul>
<li>Not by assignment</li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-8)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">rodents <span style="color: #000080;">=</span> bunnies<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-9)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">rats <span style="color: #000080;">=</span> bunnies<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Suppose you tried the following (A-10)</p>
<ul>
<li>Initializing <strong>rodents</strong> to <strong>*pt</strong> makes <strong>rodents</strong> refer to <strong>rats</strong>
<ul>
<li>Subsequently altering <strong>pt</strong> to point to <strong>bunnies</strong> does not alter the fact that <strong>rodents</strong> refers to <strong>rats</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-10)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> rats <span style="color: #000080;">=</span> <span style="color: #0000dd;">256</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> pt <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>rats<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>pt<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> bunnies <span style="color: #000080;">=</span> <span style="color: #0000dd;">128</span><span style="color: #008080;">;</span>
pt <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>bunnies<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/creating-a-reference-variable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

