page588.html

来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 68 行

HTML
68
字号
<HTML>
<HEAD>
<TITLE>Pointers Are Variables</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
 <img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms 
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html9175" HREF="page589.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page589.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html9173" HREF="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html  \n\nThis file was not retrieved by Teleport Pro, because the server reports that an error occurred that prevented retrieval.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html9167" HREF="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html  \n\nThis file was not retrieved by Teleport Pro, because the server reports that an error occurred that prevented retrieval.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html9177" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html9178" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H2><A NAME="SECTION0018110000000000000000">Pointers Are Variables</A></H2>
<P>
The C++ statement
<PRE>int* p;</PRE>
declares a <em>pointer</em><A NAME=56968>&#160;</A>.
A pointer is a variable the type of which has the form  <IMG WIDTH=18 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline73359" SRC="img2594.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2594.gif"  >
where <I>T</I> is an arbitrary type.
In this case,  <IMG WIDTH=57 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline73363" SRC="img2595.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2595.gif"  > and we say that <tt>p</tt> is a
<em>pointer</em> to <tt>int</tt>.
<P>
Since a pointer is a variable,
it has all the attributes of a variable.
In particular, it has both an <em>address</em> (<I>l</I>-value)
and a <em>value</em> (<I>r</I>-value).
<P>
Consider a C++ program that contains
the following global variable declarations:
<PRE>int i = 57;
int j = 31;
int* p = 0;
int* q = (int*) 1004;</PRE>
Suppose the four variables, <tt>i</tt>, <tt>j</tt>, <tt>p</tt> and <tt>q</tt>,
are stored in contiguous memory locations
starting at address 1000 as shown in Figure&nbsp;<A HREF="page588.html#figcpp1" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page588.html#figcpp1"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
(We assume that  <IMG WIDTH=233 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline73371" SRC="img2596.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2596.gif"  >).
Note that the variable named <tt>j</tt> has the value 31
and is found at address 1000.
Similarly, the variable named <tt>q</tt> has the value 1004
and is found at address 1012.
<P>
<P><A NAME="57084">&#160;</A><A NAME="figcpp1">&#160;</A> <IMG WIDTH=575 HEIGHT=203 ALIGN=BOTTOM ALT="figure56985" SRC="img2597.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2597.gif"  ><BR>
<STRONG>Figure:</STRONG> Memory Layout of C++ Variables and Pointers<BR>
<P>
<P>
An assignment statement such as
<PRE>i = j;</PRE>
takes the value (<I>r</I>-value) of <tt>j</tt>,
in this case 31,
and stores it in memory at the address (<I>l</I>-value) of <tt>i</tt>,
in this case 1000.
Similarly, the assignment
<PRE>p = q;</PRE>
takes the value of <tt>q</tt>,
in this case 1004,
and stores it in memory at the address of <tt>p</tt>,
in this case 1008.
<P>
<BR> <HR>
<UL> 
<LI> <A NAME="tex2html9179" HREF="page589.html#SECTION0018111000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page589.html#SECTION0018111000000000000000">Dereferencing Pointers</A>
</UL>
<HR><A NAME="tex2html9175" HREF="page589.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page589.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html9173" HREF="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html  \n\nThis file was not retrieved by Teleport Pro, because the server reports that an error occurred that prevented retrieval.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html9167" HREF="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html  \n\nThis file was not retrieved by Teleport Pro, because the server reports that an error occurred that prevented retrieval.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page587.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html9177" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html9178" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright &#169; 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.

</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

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