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

📄 right2-4.htm

📁 Visual C++面向对象程序设计教程(配套习题资源)
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                   
&nbsp;&nbsp;&nbsp; <span style="background-color: #CCFF99">第 2 章</span><span style="background-color: #CCFF99">&nbsp;                  
</span>&gt;<span style="background-color: #CCFF99"> 第 4 节 </span>&gt;<span style="background-color: #CCFF99">   
 2.4.1 </span><span style="background-color: #CCFF99">指针变量</span></p>     
<hr color="#008000" size="1">

<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"><font LANG="ZH-CN">1.  
</font><font LANG="ZH-CN">定义一个指针变量</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(1)  
<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>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000"><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>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int  
    *ptr1<font LANG="ZH-CN">,</font>*ptr2;</font></p>
  </blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(2)  
<font LANG="ZH-CN">说明</font></font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 150%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">① 
指针变量前面的“*”,表示该变量的类型为指针型变量。注意:指针变量名是</font>ptr1<font LANG="ZH-CN">、</font>ptr2<font LANG="ZH-CN">,而不是</font>*ptr1<font LANG="ZH-CN">、</font>*ptr2<font LANG="ZH-CN">。</font></font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 150%; 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: 150%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">例如“使指针移动</font>1<font LANG="ZH-CN">个位置”或“使指针值加</font>1<font LANG="ZH-CN">”,这个“</font>1<font LANG="ZH-CN">”代表什么呢?如果指针是指向一个整型变量的,那么“使指针移动</font>1<font LANG="ZH-CN">个位置”意味着移动</font>2<font LANG="ZH-CN">个字节,“使指针值加</font>1<font LANG="ZH-CN">”意味着使地址值加</font>2<font LANG="ZH-CN">个字节;如果指针是指向一个实型变量的,则增加的不是</font>2<font LANG="ZH-CN">而是</font>4<font LANG="ZH-CN">。</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2">(3)  
<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>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int  
    *ptr1,a= 3;</font></p> 
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">float 
    *ptr2,x=4.5;</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">char 
    *ptr3, ch='a';</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ptr1 
    = &amp;a ;</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ptr2 
    = &amp;x ;</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ptr3 
    = &amp;ch ;</font></p>
  </blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 150%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">上述赋值语句</font>ptr1=&amp;a<font LANG="ZH-CN">表示将变量</font>a<font LANG="ZH-CN">的地址赋给指针变量</font>ptr1<font LANG="ZH-CN">,此时</font>ptr1<font LANG="ZH-CN">就指向</font>a<font LANG="ZH-CN">。三条赋值语句产生的效果是</font>ptr1<font LANG="ZH-CN">指向</font>a<font LANG="ZH-CN">;</font>ptr2<font LANG="ZH-CN">指向</font>x<font LANG="ZH-CN">;</font>ptr3<font LANG="ZH-CN">指向</font>ch<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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
</font><font SIZE="3"><font size="2"><img border="0" src="right32.gif" width="488" height="45"></font></p>
</font>
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><u><font LANG="ZH-CN" size="2" color="#FF6600">特别需要注意的是:</font></u></p>
<p ALIGN="justify" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><u><font LANG="ZH-CN" size="2" color="#FF6600">①  
当定义指针变量时,指针变量的值是随机的,不能确定它具体的指向,必须为其赋值,才有意义。</font></u></p>
<font FACE="&#11;&#12;,Times New Roman" SIZE="3">
<p ALIGN="justify" style="line-height: 150%; margin-top: 0; margin-bottom: 0"></font><u><font size="2" color="#FF6600"><font LANG="ZH-CN">②</font> 
<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>
<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>
<blockquote>

⌨️ 快捷键说明

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