📄 right2-3.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 2 章</title>
<base target="rbottom">
<style>
<!--
.右标题 { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容 { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin:
0 }
-->
</style>
</head>
<body>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><img border="0" src="1.gif" width="63" height="70"></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">
<span style="background-color: #CCFF99">第 2 章</span><span style="background-color: #CCFF99">
</span>><span style="background-color: #CCFF99"> 第 3 节 </span>><span style="background-color: #CCFF99">
2.3.1 一维数组</span></p>
<hr color="#008000" size="1">
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">1.
一维数组的定义</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">定义一维数组的语法格式为:</font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000"><font LANG="ZH-CN">
类型标识符</font> <font LANG="ZH-CN">数组名</font>[<font LANG="ZH-CN">常量表达式</font>];</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">例如:</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
int a[10];</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">定义一个数组,数组名为</font>a<font LANG="ZH-CN">,有</font>10<font LANG="ZH-CN">个元素,每个元素的类型均为</font>int<font LANG="ZH-CN">。这</font>10<font LANG="ZH-CN">个元素分别是:</font>a[0]<font LANG="ZH-CN">、</font>a[1]<font LANG="ZH-CN">、</font>a[2]<font LANG="ZH-CN">、</font>a[3]<font LANG="ZH-CN">、</font>a[4]<font LANG="ZH-CN">、…、</font>a[8]<font LANG="ZH-CN">、</font>a[9]<font LANG="ZH-CN">。</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><u><font size="2" color="#FF6600"><font LANG="ZH-CN">注意下标从</font>0<font LANG="ZH-CN">开始,不能使用数组元素</font>a[10]<font LANG="ZH-CN">。</font></font></u></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">说明:</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">①</font>
<font LANG="ZH-CN">定义一个数组,数组名是标识符,起名规则同标识符命名规则。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">②</font>
<font LANG="ZH-CN">数组的类型,即数组元素的类型,可以是基本类型(整型、实型、字符型等),也可以是指针类型或是结构体或共同体类型。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">③</font>
<font LANG="ZH-CN">定义数组时,必须使用下标常量表达式,可以是常数或符号常量,但是不能是变量。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">④</font>
<font LANG="ZH-CN">如果定义多个相同类型的数组可以使用逗号隔开,例如:</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
int a[10],b[20];</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">⑤</font>
C++<font LANG="ZH-CN">编译器在进行编译时,为数组连续分配地址空间,分配空间的大小为:数组元素占用字节数×数组长度。</font></font></p>
<font FACE=",Times New Roman" SIZE="3">
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"></font><u><font size="2" color="#FF6600"><font LANG="ZH-CN">⑥
一定注意数组名表示数组第一个单元</font>a[0]<font LANG="ZH-CN">的地址,也就是数组的首地址。</font></font></u></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">2.
一维数组的引用</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">C++<font LANG="ZH-CN">语言规定,不能引用整个数组,只能逐个引用元素,元素引用方式为:</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000"><font LANG="ZH-CN">
数组名</font>[<font LANG="ZH-CN">下标</font>]</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">下标可以是整型常量或整型表达式,例如:</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">a[3]=a[2]+a[3*2];</font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font lang="ZH-CN" size="2">3.
一维数组的初始化</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">数组的初始化是指在定义数组时给全部数组元素或部分数组元素赋值。一维数组的初始化有下面四种情况:</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">①</font>
<font LANG="ZH-CN">在定义数组时对所有数组元素赋值。例如:</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2">
int a[10]={1,2,3,4,5,6,7,8,9,10};</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">将数组元素的初值依次放在一对花括弧内。经过上面的定义和初始化之后,</font>a[0]=1<font LANG="ZH-CN">,</font>a[1]=2<font LANG="ZH-CN">,</font>a[2]=3<font LANG="ZH-CN">,</font>a[3]=4<font LANG="ZH-CN">,</font>a[4]=5<font LANG="ZH-CN">,</font>a[5]=6<font LANG="ZH-CN">,</font>a[6]=7<font LANG="ZH-CN">,</font>a[7]=8<font LANG="ZH-CN">,</font>a[8]=9<font LANG="ZH-CN">,</font>a[9]=10<font LANG="ZH-CN">。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">②</font>
<font LANG="ZH-CN">定义时给部分元素赋值。例如:</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2">
int a[10]={1,2,3,4};</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">定义数组</font>a<font LANG="ZH-CN">有</font>10<font LANG="ZH-CN">个元素,但花括弧中只提供了</font>4<font LANG="ZH-CN">个初值,这表示只给前面</font>4<font LANG="ZH-CN">个元素赋初值,后</font>6<font LANG="ZH-CN">个元素的值为</font>0<font LANG="ZH-CN">。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">③</font>
<font LANG="ZH-CN">如果想使一个数组全部元素值为</font>0<font LANG="ZH-CN">,可以写成:</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2">
int a[10]={0,0,0,0,0,0,0,0,0,0};</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">如果定义了一个全局数组或静态数组,没有进行初始化,系统自动将数组的元素赋</font>0<font LANG="ZH-CN">。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">④</font>
<font LANG="ZH-CN">在对全部数组元素赋初值时,可以不指定数组的长度。系统会自动计算长度。例如:</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2">
int a[]={1,2,3,4,5};</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">等价于:</font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2">
int a[5]={1,2,3,4,5};</font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"> </p>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <span style="position: absolute; left: 35; top: 905"><a href="right2-2-2.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 492; top: 907"><a href="right2-3-2.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -