📄 c81.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>联合类型 </title>
<script language="javascript">
var prePage="c/c7/c76.htm";
var nextPage="c/c8/c82.htm";
function showwin(url,winname,properties){
window.open(url,winname,properties)
}
</script>
<link rel="stylesheet" href="../cstyle.css" type="text/css">
<bgsound src="../voice/c81.au" loop="1">
</head>
<body background="../img/mainback.jpg" bgproperties="fixed">
<h2 align="center"><font face="楷体_GB2312"><a name="_top"></a>8.1 联合类型</font></h2>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="33%" align="center"><a href="c81.htm#c811.html#c811">联合类型</a></td>
<td width="33%" align="center"><a href="c81.htm#c812.html#c812">sizeof 算符</a></td>
<td width="34%" align="center"><a href="c81.htm#c813.html#c813">练习题</a></td>
</tr>
</table>
<hr>
<h3><a name="c811"></a>1.联合类型</h3>
<blockquote>
<p>正如你所知道的,
结构变量是占据各自不同空间的各成员变量的集合。<br>
<br>
例如 :<br>
<img src="../img/c811.jpg" alt="c811.jpg (2884 bytes)" align="right" WIDTH="270"
HEIGHT="62">struct xtype <br>
{<br>
int i;<br>
char c[2];<br>
}xv;<br>
的内存映象如右图: <br>
<br>
然而, 有时我们想让几个变量共享同一存储区, 该怎么做呢?<br>
<br>
好, 我们现在介绍一种叫做联合的新类型来解决这个问题。<br>
<br>
<img src="../img/c812.jpg" alt="c812.jpg (3442 bytes)" align="right" WIDTH="219"
HEIGHT="85">学过联合 <font color="#FF0000">union</font> 之后 ,
你就可以象右图那样使整数 i 和字符数组 c[2] 共享同一存储区。<br>
<br>
明白了吧!
联合是由几个不同类型的不同变量共同占用的一块内存空间。<br>
<br>
那么, 怎样定义一个联合和该联合类型的变量呢? 哦!
定义一个联合和定义一个结构是一样的呀! 它们之间的不同点是用
struct 代替 union。那么, 我们就来看一下联合的定义吧! <br>
<br>
<font color="#000080">union name {<br>
成员说明<br>
成员说明<br>
. . .<br>
};</font><br>
<br>
union name 变量表; (union 是一个关键字。)<br>
<br>
让我们来看一个例子。这里是一个名字为 u 的 union 类型的定义,
它包含一个字符和一个整数。<br>
<font color="#000080">union u{<br>
int i;<br>
char c;<br>
};</font><br>
<img src="../img/c813.jpg" alt="c813.jpg (3531 bytes)" align="right" WIDTH="219"
HEIGHT="118">如下那样, 说明一个 u类型的变量 cnvt。union u cnvt; 那么, cnvt
的内存映象是怎样的呢? <br>
<br>
你也可以这样定义 cnvt:<br>
<font color="#000080">union u {<br>
int i;<br>
char c;<br>
} cnvt;</font></p>
<p>在访问联合的成员时, 采用与结构一样的算符: 点算符 '<font
color="#FF0000">.</font>' 和箭头算符 '<font color="#FF0000">-></font>'。<br>
<br>
例如:<br>
<font color="#000080">union x{<br>
int i;<br>
float f;<br>
}fi,*uptr;</font><br>
那么, 怎样把整数 10 赋值给 fi 的成员 i 呢? <br>
我们可以这样做: <font color="#FF0000">fi.i=10;</font> 此外, 你也可以这样写:
<font color="#FF0000">uptr->i=10;</font></p>
<p class="note">注意
程序员必须确保访问的联合成员的类型的一致性。即,
数据是以何种类型写入的, 读取时就要以那种类型来读。</p>
<p align="right"><a href="c81.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<hr>
<h3><a name="c812"></a>2.sizeof 算符</h3>
<blockquote>
<p>正如你已知道的那样, 结构和联合都可用来产生占空间很大的变量,
而这些变量占用的实际空间是随机器而变的。<br>
<br>
一元算符 <font color="#FF0000">sizeof</font>
可用来得到任何变量类型占用的空间大小, 包括结构, 联合,
以及用户定义的变量在内。它的作用是帮助你排除程序中依赖于机器的代码。<br>
<br>
假设这些是一个具体 C 编译器的数据类型的占空大小:</p>
<div align="center"><center><table border="5" width="84%" bgcolor="#CCFFFF"
bordercolor="#FF9933" cellspacing="0" cellpadding="0">
<tr>
<th width="30%" bgcolor="#FF9933">类型</th>
<td width="15%" align="center">char</td>
<td width="14%" align="center">int</td>
<td width="15%" align="center">long</td>
<td width="12%" align="center">float</td>
<td width="14%" align="center">double</td>
</tr>
<tr>
<th width="30%" bgcolor="#FF9933">占空大小(字节数)</th>
<td width="15%" align="center">1</td>
<td width="14%" align="center">2</td>
<td width="15%" align="center">4</td>
<td width="12%" align="center">8</td>
<td width="14%" align="center">16</td>
</tr>
</table>
</center></div><p><a
href="javascript:showwin('c81_21.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=600,height=600')">进一步看例子</a><img
src="../img/lefthand.gif" alt="lefthand.jpg (983 bytes)" WIDTH="45" HEIGHT="20"></p>
<p align="right"><a href="c81.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<hr>
<h3><a name="c813"></a>3.练习题</h3>
<blockquote>
<p><a
href="javascript:showwin('c81_31.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=600,height=600')">练习题一</a><img
src="../img/lefthand.gif" alt="lefthand.jpg (983 bytes)" WIDTH="45" HEIGHT="20"></p>
<p align="right"><a href="c81.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<p align="center"><a href="http://www.nec.sjtu.edu.cn/support/Course/C/c/c8/c82.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 + -