⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2ptrarr.html

📁 Visual C++ has been one of most effective tool for the large industrial applications. This book is t
💻 HTML
字号:
<html>
<head>
	<title>Pointers and Arrays</title>
    <meta  name="description" content="Arrays and pointers in C++">
    <meta name="keywords" content="array, pointer, string, strlen">
	<link rel="stylesheet" href="rs.css" tppabs="http://www.relisoft.com/book/rs.css">
</head>

<body background="margin.gif" tppabs="http://www.relisoft.com/book/images/margin.gif" bgcolor="#FFFFDC">

<!-- Main Table -->
<table cellpadding="6">
    <tr>
    <td width="78">
	&nbsp;
    <td>

<h3>Pointers and Arrays</h3>

<p>Arrays are passed around as pointers. In fact, a C++ array is implemented as a pointer to the first element of the array. In practical terms it means that we can access a pointer as if it were an array--that is using an index. Conversely, we can use a pointer to access elements of an array. We can increment a pointer to move from one element of the array to the next. Of course, in both cases it is our responsibility to make sure that the pointer actually points to an element of an array.

<p>A string is a good example of an array. There is a function in the standard library called <var>strlen</var> that calculates the length of a null terminated string. Let抯 write our own implementation of this function, which we will call <var>StrLen</var>

<!-- Code --><table width="100%" cellspacing=10><tr>	<td class=codetable>
<pre>int StrLen (char const str [] )
{
    for (int i = 0; str [i] != '\0'; ++i)
        continue;
    return i;
}
</pre>
</table><!-- End Code -->
<p>The <b><i>continue</i></b> keyword is used here instead of an empty body of the loop. It's less error prone this way.
<p>Here抯 the main procedure that passes an array to <var>StrLen</var>:
<!-- Code --><table width="100%" cellspacing=10><tr>	<td class=codetable>
<pre>int main ()
{
    char aString [] = "the long string";
    int len = StrLen (aString);
    cout &lt;&lt; "The length of " &lt;&lt; aString &lt;&lt; " is " &lt;&lt; len;
}</pre>
</table><!-- End Code -->

<p>We are scanning the string for a terminating null and returning the index of this null. Pretty obvious, isn抰 it?
<p>Here抯 a more traditional &quot;optimized&quot; version:

<!-- Code --><table width="100%" cellspacing=10><tr>	<td class=codetable>
<pre>int StrLen (char const * pStr)
{
    char const * p = pStr;
    while (*p++);
    return p - pStr - 1;
}</pre>
</table><!-- End Code -->

<p>We initialize <var>p</var> to point to the beginning of the string. The while loop is a little cryptic. We dereference the pointer, test it for Boolean truth and <b><i>post-increment</i></b> it, all in one statement. If the character obtained by dereferencing the pointer is different from zero (zero being equivalent to Boolean false) we will continue looping. The post-increment operator moves the pointer to the next position in the array, but only after it has been used in the expression (yielding true of false). 

<p><img src="Image2-2.gif" tppabs="http://www.relisoft.com/book/lang/pointer/images/Image2.gif" width=456 height=150 alt=" ">

<p class=caption>Figure 2. The pointer p initially points at the first character of the string &quot;Hi!&quot; at address 0xe04 (I use hexadecimal notation for addresses). Subsequent increments move it through the characters of the string until the null character is reached and processed.</p>



<p>
<!-- Sidebar -->
<table width="100%" border=0 cellpadding=5><tr>
<td width=10>&nbsp;</td>
<td bgcolor="#cccccc" class=sidebar>
<p>By the way, there is also a <b><i>pre-increment</i></b> operator that is written in front of a variable, ++p. It increments the variable <i>before</i> its value is used in the expression. 
<p>
<table border=1>
<tr>
<td colspan=2><b>Increment Operators</b> (acting on <var>i</var>)
<tr>
<td>i++      <td>post-increment
<tr>
<td>++i      <td>pre-increment
<tr>
<td colspan=2><b>Decrement Operators</b> (acting on <var>i</var>)
<tr>
<td>i--      <td>post-decrement
<tr>
<td>--i      <td>pre-decrement
</table>
</td></table>
<!-- End Sidebar -->

<p>In the spirit of terseness, I haven抰 bothered using the <var>continue</var> statement in the empty body of the loop梩he semicolon (denoting the empty statement) is considered sufficient for the old-school C programmers.

<p>Finally, we subtract the two pointers to get the number of array elements that we have gone through; and add one, since we have overshot the null character (the last test accesses the null character, and then it increments the pointer anyway). By the way, I never get this part right the first time around. I had to pay the penalty of an additional edit-compile-run cycle. If you have problems understanding this second implementation, you抮e lucky. I won抰 have to convince you any further not to write code like this. 
<p>The question is: Will you prefer to write the simpler and more readable index implementation of procedures like <var>StrLen</var> after seeing both versions? If you have answered <i>yes</i>, you may go directly to the following paragraph. If you want more gory details, click here and see some <a href="javascript:if(confirm('http://www.relisoft.com/book/lang/pointer/asm.html  \n\nThis file was not retrieved by Teleport Pro, because it is linked too far away from its Starting Address. If you increase the in-domain depth setting for the Starting Address, this file will be queued for retrieval.  \n\nDo you want to open it from the server?'))window.location='http://www.relisoft.com/book/lang/pointer/asm.html'" tppabs="http://www.relisoft.com/book/lang/pointer/asm.html">assembly code</a>.
<p>&nbsp;</p>
<!-- Sidebar -->
<table width="100%" border=0 cellpadding=5><tr>
<td width=10>
<td bgcolor="#cccccc" class=sidebar>

<p><b>Soapbox</b>
<p>The art of programming is in a very peculiar situation. It is developing so fast, that people who started programming when C was in its infancy are still very active in the field. In other sciences a lot of progress was made through natural attrition. The computer revolution happened well within one generation. Granted, a lot of programmers made enough money to be able to afford doing volunteer work for the rest of their lives. Still, many others are carrying around their old (although only a few years old) bag of tricks that they learned when they were programming XT抯 with 64k memory. 
<p>New programmers learn programming from the classics like Kernighan and Ritchie抯 &quot;The C programming language.&quot; It抯 a great book, don抰 get me wrong, but it teaches the programming style of the times long gone.
<p>The highest authority in algorithms and data structures is Donald Knuth抯 great classic &quot;The Art of Programming.&quot; It抯 a beautiful and very thorough series of scientific books. However, I抳e seen C implementations of quicksort that were based on the algorithms from these books. They were pre-structured-programming monstrosities.
<p>If your compiler is unable to optimize the human readable, maintainable version of the algorithm, and you have to double as a human compiler-- <b>buy a new compiler!</b> Nobody can afford human compilers any more. So, have mercy on yourself and your fellow programmers who will have to look at your code.

<td width=10>
</table>
<!-- End Sidebar -->
<p>&nbsp;</p>

<tr>
<td class=margin valign=top>
<img src="bug.gif" tppabs="http://www.relisoft.com/book/images/bug.gif" width=25 height=25 border=0 alt="Rule of thumb.">
<td>


<!-- Definition -->
<p>
<table border=4 cellpadding=10><tr>
	<td bgcolor="#ffffff" class=deftable>

Don抰 use pointers where an index will do.</table>
<!-- End Definition -->



</table>
<!-- End Main Table -->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -