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

📄 c64.htm

📁 经典c语言教程
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>指针和数组 </title>
<script language="javascript">
   var prePage="http://www.nec.sjtu.edu.cn/support/Course/C/c/c6/c/c6/c63.htm";
   var nextPage="c/c6/c65.htm";
</script>

<link rel="stylesheet" href="../cstyle.css" type="text/css">
</head>

<body background="../img/mainback.jpg" bgproperties="fixed">

<h2 align="center"><font face="楷体_GB2312"><a name="_top"></a>6.4 指针和数组</font></h2>

<table border="0" width="100%">
  <tr>
    <td width="33%"><p align="center"><a href="c64.htm#c641.html#c641">指向数组的指针和数组下标</a> 
    &nbsp; </td>
    <td width="33%"><p align="center"><a href="c64.htm#c642.html#c642">指向字符串的指针</a></td>
    <td width="34%"><p align="center"><a href="c64.htm#c643.html#c643">指针数组 </a></td>
  </tr>
</table>

<hr>

<h3><a name="c641"></a>1.指向数组的指针和数组下标&nbsp; </h3>

<blockquote>
  <p class="note">在 C 语言中, 
  指针的最一般用途是作为指向数组的指针。使用指向数组的指针的主要理由是为了表示上的方便和程序的高效率。一般而言, 
  使用指向数组的指针产生的代码能节约内存空间, 并且运行速度快。</p>
  <p>定义: <br>
  int v[100];<br>
  int *v_pointer;<br>
  <br>
  如要把 v_pointer 指向数组 v 的第一个元素, 使用语句 v_pointer=&amp;v[0];<br>
  如果我们用另一种方法来设置: v_pointer=v;<br>
  这两句语句的效果是完全一样的!!<br>
  假定 v_pointer 指向 v[1], 那么, 语句:&nbsp; *v_pointer=100; 
  与下面的语句的效果一样。<br>
  v[1]=100;<br>
  如果我们设置: v_pointer=v, 那么, 下面的关系成立: <br>
  *v_pointer == v[0];<br>
  *(v_pointer+1) == v[1];<br>
  *(v_pointer+i) == v[i];<br>
  <br>
  一般而言, 如果我们这样来设置:<br>
  v_pointer=&amp;v[n];<br>
  其结果<br>
  *v_pointer == v[n];<br>
  *(v_pointer-i) == v[n-i];<br>
  *(v_pointer+i) == v[n+i] <br>
  </p>
  <p align="right"><a href="c64.htm#_top.html#_top">返回页首</a></p>
</blockquote>

<hr>

<h3><a name="c642"></a>2.指向字符串的指针</h3>

<blockquote>
  <p class="note">指向数组的指针最常见的应用是指向字符串的指针。</p>
  <p>为了说明使用指向字符串的指针会给处理带来多大方便, 
  我们编写了一个称作为 copy_string 的函数, 其功能 
  是把一个字符串拷贝给另一个。</p>
  <p>1. 如果采用一般的数组下标方法, 那么函数将会是如下这样的:<br>
  void copy_string(char from[], char to[])<br>
  {<br>
  &nbsp;&nbsp;&nbsp; int i;<br>
  &nbsp;&nbsp;&nbsp; for(i=0;from[i]!='\0';++i)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to[i]=from[i];<br>
  &nbsp;&nbsp;&nbsp; to[i]='\0';<br>
  }<br>
  <br>
  2. 如果使用指针的话, 那么函数会是如下这样的:<br>
  void copy_string(char *from, char *to)<br>
  {<br>
  &nbsp;&nbsp;&nbsp; for( ;*from!='\0';++from,++to)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *to=*from;<br>
  &nbsp;&nbsp;&nbsp; *to='\0';<br>
  }</p>
  <p class="note">注意: '\0'是字符串的结束符!</p>
  <p>比较: 在第二个程序中, 我们不再需要下标变量 i。这是一个更有效的程序!</p>
  <table border="5" width="78%" bgcolor="#CCFFFF" bordercolor="#FF9933" cellspacing="0"
  cellpadding="0">
    <tr>
      <td width="63%">main()<br>
      {<br>
      &nbsp; void copy_string(char *, char *);<br>
      &nbsp; static char string[50];<br>
      &nbsp;&nbsp;&nbsp; copy_string(&quot;So is this&quot;,string);<br>
      &nbsp;&nbsp;&nbsp; printf(&quot; %s\n&quot;,string);<br>
      }<br>
      void copy_string(char *from, char *to)<br>
      {<br>
      &nbsp;&nbsp;&nbsp; for( ;*from!='\0';++from,++to)<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *to=*from;<br>
      &nbsp;&nbsp;&nbsp; *to='\0';<br>
      }</td>
      <td width="37%">说明: 左边的程序调用了下面这个函数:<br>
      copy_string(\&quot;So is this\&quot;,string);<br>
      在此, 字符串常数作为实参传递给函数, 
      实际上传送的是指向它的指针。这一情况并非本例所特有的。在 C语言中, 
      每当用到字符串时, 实际上用的是指向该字符串的指针。</td>
    </tr>
  </table>
  <p><br>
  更详细地说, 如果我们定义了:<br>
  char *text_pointer;<br>
  那么, 语句: text_pointer=&quot;A char-string&quot;; 把指向字符串 &quot;A 
  char-string&quot; 的指针赋值给 text_pointer。<br>
  但是, 如果定义如下: <br>
  char text[80];<br>
  那么, 语句: text=&quot;This is not valid&quot;; 是不正确的!!<br>
  </p>
  <p class="note">注意: 字符串和字符数组不一样!!</p>
  <p align="right"><br>
  <a href="c64.htm#_top.html#_top">返回页首</a></p>
</blockquote>

<hr>

<h3><a name="c643"></a>3.指针数组 </h3>

<blockquote>
  <p>说明: 数组的元素也可以是指针。我们可以这样来定义: char *days[]; 
  我们可以如下这样, 对指针数组进行初始化:<br>
  static char *days[]=<br>
  {&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;, 
  &quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;};<br>
  数组 days 有七个元素。days 
  的每个元素分别是一个指向字符串常数的指针, 如 day[0] 
  指向的内容为 Sunday\0。&quot;</p>
  <p align="right"><a href="c64.htm#_top.html#_top">返回页首</a></p>
</blockquote>

<p align="center"><a href="http://www.nec.sjtu.edu.cn/support/Course/C/c/c6/c65.htm"><img src="../img/next.gif" width="145" height="30"
alt="next.gif (3633 bytes)" border="0"></a></p>
</body>
</html>

⌨️ 快捷键说明

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