📄 right2-3-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.3 字符数组</span></p>
<hr color="#008000" size="1">
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 10; 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 LANG="ZH-CN" size="2">1.
字符数组的定义</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>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">char
<font LANG="ZH-CN">数组名</font>[<font LANG="ZH-CN">常量表达式</font>];</font></p>
</blockquote>
</blockquote>
<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">
char
ch[12];</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
ch[0]='H';
ch[1]='o'; ch[2]='w'; ch[3]=' '; ch[4]='a'; ch[5]='r'; ch[6]='e';ch[7]=' ';</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
ch[8]='y';
ch[9]='o'; ch[10]='u'; ch[11]='\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>ch<font LANG="ZH-CN">,包含</font>12<font LANG="ZH-CN">个元素。赋值以后的状态如图所示。</font></font></p>
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><center>
<table BORDER="1" CELLSPACING="1" WIDTH="440">
<tr>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[0]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[1]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[2]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[3]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[4]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[5]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[6]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[7]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[8]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[9]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[10]</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[11]</font></td>
</tr>
<tr>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">H</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">o</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">w</font></td>
<td WIDTH="100" VALIGN="TOP">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"> </font></p>
</td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">a</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">r</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">e</font></td>
<td WIDTH="100" VALIGN="TOP">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"> </font></p>
</td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">y</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">o</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">u</font></td>
<td WIDTH="100" VALIGN="TOP">
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">\0</font></td>
</tr>
</table>
</center>
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></p>
<p ALIGN="JUSTIFY" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">2.
字符数组的引用</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 LANG="ZH-CN" size="2">3.
字符数组的初始化</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>
<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">
static
char c[10] = {'I',' ','a','m',' ','h','a','p','p','y'};</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>'\0'<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">
static
char c[10] = {'c',' ','p','r','o','g','r','a','m'};</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c[9]='\0'<font LANG="ZH-CN">,即</font>c[9]=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 LANG="ZH-CN" size="2">指定初值时,若未指定数组长度,则长度等于初值个数。例如:</font></p>
<p ALIGN="JUSTIFY" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2">
char
c[ ] = {'I',' ','a','m',' ','h','a','p','p','y'};</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">
char
c[10 ] = {'I',' ','a','m',' ','h','a','p','p','y'};</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">
char
c[] = {"I am happy"};</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">
char
c[] = "I am happy";</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>0<font LANG="ZH-CN">)。但字符数组并不要求其最后一个元素是</font>'\0'<font LANG="ZH-CN">,例如:</font></font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">char
c[] = {"China"};
<img border="0" src="right29.gif" width="161" height="36"></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">char
c[5] = {"China"};
<img border="0" src="right31.gif" width="137" height="36"></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">char
c[8] = {"China"};
<img border="0" src="right30.gif" width="198" height="35"></font></p>
</blockquote>
</blockquote>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <span style="position: absolute; left: 41; top: 966"><a href="right2-3-2.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 493; top: 967"><a href="right2-4.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 + -