📄 [c++对象模型][3]指针与数组 - itech's blog - 博客园.htm
字号:
style="COLOR: #000000">-></SPAN><SPAN
style="COLOR: #000000">name </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">jason</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">; </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 此时的name指向了常量区,所以name指针不需要程序员释放。</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"> free( willy );<BR> willy </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> NULL;<BR>}</SPAN></SPAN></DIV>
<P> </P>
<P><SPAN style="COLOR: #000000">malloc/free 和new/delete的区别:</SPAN></P>
<P><SPAN style="COLOR: #ff0000">1)new/delete是保留字,不需要头文件支持.
malloc/free需要头文件库函数支持. 使用malloc/free需要包含 #include<cstdlib>
或<stdlib><SPAN class=co2>.</SPAN></SPAN></P>
<P><SPAN style="COLOR: #ff0000"><SPAN class=co2>2) </SPAN>new
建立的是一个对象,new会根据对象计算大小,直接返回对象的指针,当使用完毕后调用delete来释放,但malloc分配的是一块内存,需要用户制定所要分配内存的大小,而且返回的均为void的指针,使用时需要相应的强制类型转化,使用结束后调用free来释放内存.</SPAN></P>
<P><SPAN
style="COLOR: #ff0000">3)new/delete的使用除了分配内存和释放,还调用了类型的构造函数和析构函数,而malloc/free只是简单的分配和释放内存。</SPAN></P>
<P>二 数组与指针</P>
<P>C++的数组经常需要和指针来结合使用,下面来进行相关的强化训练。实例如下:</P>
<P> </P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_153047 style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_153047').style.display='none'; document.getElementById('Code_Open_Image_153047').style.display='inline'; document.getElementById('Code_Open_Text_153047').style.display='inline';"
height=16 src="[C++对象模型][3]指针与数组 - iTech's Blog - 博客园.files/ContractedBlock.gif"
width=11 align=top><IMG id=Code_Open_Image_153047
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_153047').style.display='none'; getElementById('Code_Closed_Image_153047').style.display='inline'; getElementById('Code_Closed_Text_153047').style.display='inline';"
height=16
src="[C++对象模型][3]指针与数组 - iTech's Blog - 博客园.files/ExpandedBlockStart.gif"
width=11 align=top><SPAN class=cnblogs_code_Collapse
id=Code_Closed_Text_153047>Code</SPAN><SPAN id=Code_Open_Text_153047><BR><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN
style="COLOR: #0000ff">#pragma</SPAN><SPAN
style="COLOR: #000000"> once</SPAN><SPAN
style="COLOR: #000000"><BR>#include </SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN
style="COLOR: #000000">iostream</SPAN><SPAN
style="COLOR: #000000">></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff">using</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">namespace</SPAN><SPAN
style="COLOR: #000000"> std;<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> PrintArray(</SPAN><SPAN
style="COLOR: #0000ff">double</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">p, </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> num)<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> i </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">0</SPAN><SPAN
style="COLOR: #000000">; i </SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN
style="COLOR: #000000"> num; </SPAN><SPAN
style="COLOR: #000000">++</SPAN><SPAN
style="COLOR: #000000">i)<BR> {<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> p[i] </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000">;<BR> }<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">The array is end!</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR>}<BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> PrintArray(</SPAN><SPAN
style="COLOR: #0000ff">double</SPAN><SPAN
style="COLOR: #000000"> arr[</SPAN><SPAN
style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">]) <BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> i </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">0</SPAN><SPAN
style="COLOR: #000000">; i </SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">; </SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN
style="COLOR: #000000">i)<BR> {<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">(arr</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN
style="COLOR: #000000">i)</SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN
style="COLOR: #008000">arr[i]</SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000">;<BR> }<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">The array is end!</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR>}<BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> ChangeArray(</SPAN><SPAN
style="COLOR: #0000ff">double</SPAN><SPAN
style="COLOR: #000000"> arr[</SPAN><SPAN
style="COLOR: #800080">3</SPAN><SPAN style="COLOR: #000000">]) </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 数组传参为传指针,所以函数内可以修改</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000">{<BR> </SPAN><SPAN
style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> i </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">0</SPAN><SPAN
style="COLOR: #000000">; i </SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">; </SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN
style="COLOR: #000000">i)<BR> {<BR> arr[i] </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">10</SPAN><SPAN
style="COLOR: #000000">;<BR> }<BR>}<BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> PrintArray(</SPAN><SPAN
style="COLOR: #0000ff">double</SPAN><SPAN
style="COLOR: #000000"> arr[</SPAN><SPAN
style="COLOR: #800080">3</SPAN><SPAN style="COLOR: #000000">][</SPAN><SPAN
style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">])<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> i </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">0</SPAN><SPAN
style="COLOR: #000000">; i </SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">; </SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN
style="COLOR: #000000">i)<BR> </SPAN><SPAN
style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> j </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">0</SPAN><SPAN
style="COLOR: #000000">; j </SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">; </SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN
style="COLOR: #000000">j)<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> arr[i][j] </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000">;<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">The array is end!</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR>}<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> GetLength(){</SPAN><SPAN
style="COLOR: #0000ff">return</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">;}<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> TestArray()<BR>{<BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 数组的定义和初始化</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">short</SPAN><SPAN
style="COLOR: #000000"> months[</SPAN><SPAN
style="COLOR: #800080">12</SPAN><SPAN style="COLOR: #000000">] </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> {</SPAN><SPAN
style="COLOR: #800080">31</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">28</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">31</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">30</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">31</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">30</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">31</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">31</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">30</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">31</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">30</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800080">31</SPAN><SPAN
style="COLOR: #000000">};<BR> </SPAN><SPAN
style="COLOR: #0000ff">double</SPAN><SPAN
style="COLOR: #000000"> arr[</SPAN><SPAN
style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">]; <BR> arr[</SPAN><SPAN
style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">] </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">1.0</SPAN><SPAN
style="COLOR: #000000">;<BR> arr[</SPAN><SPAN
style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">] </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">2.0</SPAN><SPAN
style="COLOR: #000000">;<BR> arr[</SPAN><SPAN
style="COLOR: #800080">2</SPAN><SPAN style="COLOR: #000000">] </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">3.0</SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -