📄 page_609.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_609</title> <link rel="stylesheet" href="reset.css" type="text/css" media="all"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <table summary="top nav" border="0" width="100%"> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_608.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_609</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_610.html">next page ></a></td> </tr> <tr> <td align="left" colspan="3" style="background: #ffffff; padding: 20px;"> <table border="0" width="100%" cellpadding="0"><tr><td align="center"> <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td> <td align="right"></td> </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 609</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">In the formal parameter list, the declaration of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">arr</font><font face="Times New Roman, Times, Serif" size="3"> does not include a size within the brackets. If you include a size, the compiler ignores it. The compiler only wants to know that it is a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">float</font><font face="Times New Roman, Times, Serif" size="3"> array, not a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">float</font><font face="Times New Roman, Times, Serif" size="3"> array of any particular size. Therefore, you must include a second parameter-the number of array elements-in order for this function to work correctly.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">The calling code can invoke the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ZeroOut</font><font face="Times New Roman, Times, Serif" size="3"> function for a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">float</font><font face="Times New Roman, Times, Serif" size="3"> array of any size. The following code fragment makes function calls to zero out two arrays of different sizes. Notice how an array parameter is declared in a function prototype.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">void聽ZeroOut(聽float[],聽int聽);聽聽聽聽//聽Function聽prototype<br />聽聽.<br />聽聽.<br />聽聽.<br /><br />int聽main()<br />{<br />聽聽聽聽float聽velocity[30];<br />聽聽聽聽float聽refractionAngle[9000];<br />聽聽聽聽聽聽.<br />聽聽聽聽聽聽.<br />聽聽聽聽聽聽.<br />聽聽聽聽ZeroOut(velocity,聽30);<br />聽聽聽聽ZeroOut(refractionAngle,聽9000);<br />聽聽聽聽聽聽.<br />聽聽聽聽聽聽.<br />聽聽聽聽聽聽.<br />}</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">With simple variables, pass-by-value prevents a function from modifying the caller's actual parameter. You cannot pass arrays by value in C++, but you can still prevent the function from modifying the caller's array. To do so, you use the reserved word </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">const</font><font face="Times New Roman, Times, Serif" size="3"> in the declaration of the formal parameter. Below is a function that copies one </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">int</font><font face="Times New Roman, Times, Serif" size="3"> array into another. The first parameter-the destination array-is expected to be modified, but the second array is not.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">void聽Copy(聽/*聽out聽*/聽聽聽聽聽聽聽聽聽int聽destination[],<br />聽聽聽聽聽聽聽聽聽聽聽/*聽in聽*/聽聽聽聽const聽int聽source[],<br />聽聽聽聽聽聽聽聽聽聽聽/*聽in聽*/聽聽聽聽聽聽聽聽聽聽int聽size聽聽聽聽聽聽聽聽聽聽)<br />{<br />聽聽聽聽int聽i;<br /><br />聽聽聽聽for聽(i聽=聽0;聽i聽<聽size;聽i++)<br /><br />聽聽聽聽聽聽聽聽聽聽聽聽//聽Invariant聽(prior聽to聽test):<br />聽聽聽聽聽聽聽聽聽聽聽聽//聽聽聽聽聽For聽all聽k,聽where聽0聽<=聽k聽<=聽i-l,<br />聽聽聽聽聽聽聽聽聽聽聽聽//聽聽聽聽聽聽聽聽destination[k]聽==聽source[k]<br />聽聽聽聽聽聽聽聽聽聽聽聽//聽聽&&聽0聽<=聽i聽<=聽size</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>聽 </td> </tr> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_608.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_609</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_610.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -