?? page589.html
字號:
<HTML>
<HEAD>
<TITLE>Dereferencing Pointers</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="tex2html9186" HREF="page590.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page590.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="tex2html9184" HREF="page588.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page588.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="tex2html9180" HREF="page588.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page588.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="tex2html9188" 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="tex2html9189" 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>
<H3><A NAME="SECTION0018111000000000000000">Dereferencing Pointers</A></H3>
<P>
A variable of type <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" >
is said to <em>point</em> at an object of type <I>T</I>.
What this really means is that the <em>value</em> of the <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" > variable
is the address of another variable of type <I>T</I>.
For example,
in Figure <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> the variable <tt>q</tt> is has type <tt>int*</tt>,
i.e., it is a <em>pointer</em> to an <tt>int</tt>.
The <em>value</em> of <tt>p</tt> is 1004,
which is the <em>address</em> of the variable <tt>j</tt>.
<P>
To access the variable to which a pointer points,
we must <em>dereference</em><A NAME=57106> </A> the pointer.
E.g., to dereference the pointer <tt>q</tt> we write <tt>*q</tt>.
Whereas <tt>q</tt> denotes the pointer variable itself,
<tt>*q</tt> denotes the <tt>int</tt> to which the pointer points.
<P>
If we use <tt>*q</tt> in a context where an <I>r</I>-value is expected,
then we get the <I>r</I>-value of the variable to which <tt>q</tt> points.
E.g., since <tt>q</tt> points to the variable <tt>j</tt>,
the assignment
<PRE>i = *q;</PRE>
takes the <I>r</I>-value of <tt>j</tt> (31) and assigns it to <tt>i</tt>.
<P>
Similarly, if we use <tt>*q</tt> in a context where an <I>l</I>-value is required,
then we get the <I>l</I>-value of the variable to which <tt>q</tt> points.
E.g., since <tt>q</tt> points to the variable <tt>j</tt>,
the assignment
<PRE>*q = 31;</PRE>
takes the <I>l</I>-value of <tt>j</tt> (1004) and stores 31 at that location.
Notice that the <I>l</I>-value of <tt>*q</tt>
is identical to the <I>r</I>-value of <tt>q</tt>.
<P>
<HR><A NAME="tex2html9186" HREF="page590.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page590.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="tex2html9184" HREF="page588.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page588.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="tex2html9180" HREF="page588.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page588.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="tex2html9188" 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="tex2html9189" 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 © 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 + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -