﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-海阔天空-文章分类-语言基础</title><link>http://www.blogjava.net/shiliqiang/category/40697.html</link><description>I'm on my way!</description><language>zh-cn</language><lastBuildDate>Sun, 20 Sep 2009 21:07:24 GMT</lastBuildDate><pubDate>Sun, 20 Sep 2009 21:07:24 GMT</pubDate><ttl>60</ttl><item><title>对和栈的区别</title><link>http://www.blogjava.net/shiliqiang/articles/295780.html</link><dc:creator>石头@</dc:creator><author>石头@</author><pubDate>Sun, 20 Sep 2009 14:15:00 GMT</pubDate><guid>http://www.blogjava.net/shiliqiang/articles/295780.html</guid><wfw:comment>http://www.blogjava.net/shiliqiang/comments/295780.html</wfw:comment><comments>http://www.blogjava.net/shiliqiang/articles/295780.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shiliqiang/comments/commentRss/295780.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shiliqiang/services/trackbacks/295780.html</trackback:ping><description><![CDATA[<div id="message7049011" class="t_msgfont">一、预备知识—程序的内存分配 <br />
<br />
由C/C++编译的程序占用的内存分为以下几个部分 <br />
1、栈区(stack)： 由编译器自动分配释放 ，存放函数的参数值，局部变量的值等。其操作方式类似于数据结构中的栈。 <br />
2、堆区(heap)： 一般由程序员分配释放， 若程序员不释放，程序结束时可能由OS回收。注意它与数据结构中的堆是两回事，分配方式倒是类似于链表。 <br />
3、全局区(static)： 全局变量和静态变量的存储是放在一块的，初始化的全局变量和静态变量在一块区域， 未初始化的全局变量和未初始化的静态变量在相邻的另一块区域，程序结束后有系统释放 。 <br />
4、文字常量区： 常量字符串就是放在这里的， 程序结束后由系统释放。 <br />
5、程序代码区： 存放函数体的二进制代码。 <br />
<br />
Example: <br />
<br />
int a = 0; // 全局初始化区 <br />
char *p1; // 全局未初始化区 <br />
main() <br />
{ <br />
int b; // 栈 <br />
char s[] = "abc"; // 栈 <br />
char *p2; // 栈 <br />
char *p3 = "123456"; // 123456\0在常量区，p3在栈上。 <br />
static int c =0； // 全局(静态)初始化区 <br />
p1 = (char *)malloc(10); <br />
p2 = (char *)malloc(20); // 分配得来得10和20字节的区域就在堆区。 <br />
strcpy(p1, "123456"); // 123456\0放在常量区，编译器可能会将它与p3所指向的"123456"优化成一个地方。 <br />
} <br />
<br />
<br />
二、堆和栈的理论知识 <br />
<br />
2.1 申请方式 <br />
栈: 由系统自动分配。 例如，声明在函数中一个局部变量 int b; 系统自动在栈中为b开辟空间 <br />
堆: 需要程序员自己申请，并指明大小，在c中malloc函数：如p1 = (char *)malloc(10); 在C++中用new运算符 如p2 = (char *)malloc(10); 但是注意p1、p2本身是在栈中的。 <br />
<br />
2.2 申请后系统的响应 <br />
栈：只要栈的剩余空间大于所申请空间，系统将为程序提供内存，否则将报异常提示栈溢出。 <br />
堆：首先应该知道操作系统有一个记录空闲内存地址的链表，当系统收到程序的申请时，
会遍历该链表，寻找第一个空间大于所申请空间的堆结点，然后将该结点从空闲结点链表中删除，并将该结点的空间分配给程序，另外，对于大多数系统，会在这块
内存空间中的首地址处记录本次分配的大小，这样，代码中的delete语句才能正确的释放本内存空间。另外，由于找到的堆结点的大小不一定正好等于申请的
大小，系统会自动的将多余的那部分重新放入空闲链表中。 <br />
<br />
2.3 申请大小的限制 <br />
栈：在Windows下,栈是向低地址扩展的数据结构，是一块连续的内存区域。这句话的意思是栈顶的地址和栈的最大容量是系统预先规定好的，在
WINDOWS下，栈的大小是2M(也有的说是1M，总之是一个编译时就确定的常数)，如果申请的空间超过栈的剩余空间时，将提示overflow。因
此，能从栈获得的空间较小。 <br />
堆：堆是向高地址扩展的数据结构，是不连续的内存区域。这是由于系统是用链表来存储的空闲内存地址的，自然是不连续的，而链表的遍历方向是由低地址向高地址。堆的大小受限于计算机系统中有效的虚拟内存。由此可见，堆获得的空间比较灵活，也比较大。 <br />
<br />
2.4 申请效率的比较： <br />
栈：由系统自动分配，速度较快。但程序员是无法控制的。 <br />
堆：是由new分配的内存，一般速度比较慢，而且容易产生内存碎片,不过用起来最方便。 <br />
另外，在WINDOWS下，最好的方式是用VirtualAlloc分配内存，他不是在堆，也不是在栈是直接在进程的地址空间中保留一快内存，虽然用起来最不方便。但是速度快，也最灵活。 <br />
<br />
2.5 堆和栈中的存储内容 <br />
栈：
在函数调用时，第一个进栈的是主函数中后的下一条指令(函数调用语句的下一条可执行语句)的地址，然后是函数的各个参数，在大多数的C编译器中，参数是由
右往左入栈的，然后是函数中的局部变量。注意静态变量是不入栈的。当本次函数调用结束后，局部变量先出栈，然后是参数，最后栈顶指针指向最开始存的地址，
也就是主函数中的下一条指令，程序由该点继续运行。 <br />
堆：一般是在堆的头部用一个字节存放堆的大小。堆中的具体内容有程序员安排。 <br />
<br />
2.6 存取效率的比较 <br />
char s1[] = "aaaaaaaaaaaaaaa"; <br />
char *s2 = "bbbbbbbbbbbbbbbbb"; <br />
aaaaaaaaaaa是在运行时刻赋值的； <br />
而bbbbbbbbbbb是在编译时就确定的； <br />
但是，在以后的存取中，在栈上的数组比指针所指向的字符串(例如堆)快。 <br />
比如： <br />
#include <br />
void main() <br />
{ <br />
char a = 1; <br />
char c[] = "1234567890"; <br />
char *p ="1234567890"; <br />
a = c[1]; <br />
a = p[1]; <br />
return; <br />
} <br />
对应的汇编代码 <br />
10: a = c[1]; <br />
00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh] <br />
0040106A 88 4D FC mov byte ptr [ebp-4],cl <br />
11: a = p[1]; <br />
0040106D 8B 55 EC mov edx,dword ptr [ebp-14h] <br />
00401070 8A 42 01 mov al,byte ptr [edx+1] <br />
00401073 88 45 FC mov byte ptr [ebp-4],al <br />
第一种在读取时直接就把字符串中的元素读到寄存器cl中，而第二种则要先把指针值读到edx中，在根据edx读取字符，显然慢了。 <br />
<br />
2.7 小结： <br />
堆和栈的区别可以用如下的比喻来看出：
使用栈就象我们去饭馆里吃饭，只管点菜(发出申请)、付钱、和吃(使用)，吃饱了就走，不必理会切菜、洗菜等准备工作和洗碗、刷锅等扫尾工作，他的好处是
快捷，但是自由度小。 使用堆就象是自己动手做喜欢吃的菜肴，比较麻烦，但是比较符合自己的口味，而且自由度大。 <br />
<br />
还有就是函数调用时会在栈上有一系列的保留现场及传递参数的操作。栈的空间大小有限定，VC的缺省是2M。栈不够用的情况一般是程序中分配了大量数组和递
归函数层次太深。有一点必须知道，当一个函数调用完返回后它会释放该函数中所有的栈空间。栈是由编译器自动管理的，不用你操心。堆是动态分配内存的，并且
你可以分配使用很大的内存。但是用不好会产生内存泄漏。并且频繁地malloc和free会产生内存碎片(有点类似磁盘碎片)，因为C分配动态内存时是寻
找匹配的内存的。而用栈则不会产生碎片。在栈上存取数据比通过指针在堆上存取数据快些。一般大家说的堆栈和栈是一样的，就是栈(stack)，而说堆时才
是堆heap。栈是先入后出的，一般是由高地址向低地址生长。<br />
<br />
<br />
摘自：http://linux.chinaunix.net/bbs/thread-1119653-1-1.html<br />
</div>
<img src ="http://www.blogjava.net/shiliqiang/aggbug/295780.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shiliqiang/" target="_blank">石头@</a> 2009-09-20 22:15 <a href="http://www.blogjava.net/shiliqiang/articles/295780.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>能测试你c语言功底的几个问题</title><link>http://www.blogjava.net/shiliqiang/articles/291970.html</link><dc:creator>石头@</dc:creator><author>石头@</author><pubDate>Thu, 20 Aug 2009 08:24:00 GMT</pubDate><guid>http://www.blogjava.net/shiliqiang/articles/291970.html</guid><wfw:comment>http://www.blogjava.net/shiliqiang/comments/291970.html</wfw:comment><comments>http://www.blogjava.net/shiliqiang/articles/291970.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shiliqiang/comments/commentRss/291970.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shiliqiang/services/trackbacks/291970.html</trackback:ping><description><![CDATA[下面有几个简单测试将能发现你对C语言的掌握情况。<br />
int x=35;<br />
char str[10];<br />
//问：strlen(str)和sizeof(str)的值分别是多少？<br />
strcpy(str,"www.it315.org"/*共13个字母*/);<br />
//问:此时x和strlen(str)的值分别是多少？<br />
str="it315.org";//编译能通过吗？<br />
char *pstr;<br />
strcpy(pstr,"http://www.it315.org");<br />
//上句编译能通过吗？运行时有问题吗？<br />
const char *p1;<br />
char * const p2;<br />
//上面两句有什么区别吗？<br />
p1=(const char *)str;<br />
//如果是p1=str；编译能够通过吗？明白为什么要类型转换？类型转换的本质是什么？<br />
strcpy(p1,"abc");//编译能够通过吗？<br />
printf("%d",str);//有问题吗？<br />
pstr=3000;//编译能过吗？如果不行，该如何修改以保证编译通过呢？<br />
long y=(long)pstr;//可以这样做吗？<br />
int *p=str;<br />
*p=0x00313200;<br />
printf("%s",str);//会是什么效果？提示0x31对应字符'1',0x32对应字符'2'。<br />
p=3000;//p+1的结果会是多少？<br />
char *pc=new char[100];//上述语句在内存中占据几个内存块，怎样的布局情况？<br />
void test(char **p)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *p=new char[100];<br />
}//这个编译函数有问题吗？外面要调用这个函数，该怎样传递参数？<br />
//能明白typedef int (*PFUN)(int x,int y)及其作用吗？<br />
<br />
<br />
摘自：http://blog.csdn.net/zhangxiaoxiang/archive/2006/05/17/742931.aspx<br />
<img src ="http://www.blogjava.net/shiliqiang/aggbug/291970.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shiliqiang/" target="_blank">石头@</a> 2009-08-20 16:24 <a href="http://www.blogjava.net/shiliqiang/articles/291970.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>void 类型的指针</title><link>http://www.blogjava.net/shiliqiang/articles/290353.html</link><dc:creator>石头@</dc:creator><author>石头@</author><pubDate>Sat, 08 Aug 2009 09:09:00 GMT</pubDate><guid>http://www.blogjava.net/shiliqiang/articles/290353.html</guid><wfw:comment>http://www.blogjava.net/shiliqiang/comments/290353.html</wfw:comment><comments>http://www.blogjava.net/shiliqiang/articles/290353.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shiliqiang/comments/commentRss/290353.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shiliqiang/services/trackbacks/290353.html</trackback:ping><description><![CDATA[<div class="posttitle">
<a id="viewpost1_TitleUrl" class="singleposttitle" href="http://www.cppblog.com/dragon/archive/2008/09/02/60760.html">void指针</a>
</div>
指针有两个属性:指向变量/对象的<font style="line-height: 1.3em;" color="#ff0000">地址</font>
<div class="singlepost"><wbr>和<font style="line-height: 1.3em;" color="#ff0000">长度</font><wbr> <br />
但是指针只存储地址,长度则取决于指针的类型 <br />
编译器根据指针的类型从指针指向的地址向后寻址 <br />
指针类型不同则寻址范围也不同,比如: <br />
int*从指定地址向后寻找4字节作为变量的存储单元 <br />
double*从指定地址向后寻找8字节作为变量的存储单元 <br />
<br />
1.void指针是一种特别的指针 <br />
&nbsp;&nbsp; void *vp <br />
&nbsp;&nbsp;//说它特别是因为它没有类型 <br />
&nbsp;&nbsp;//或者说这个类型不能判断出指向对象的长度 <br />
<br />
2.任何指针都可以赋值给void指针 <br />
&nbsp;&nbsp;type *p; <br />
&nbsp;&nbsp;vp=p; <br />
&nbsp;&nbsp;//不需转换 <br />
&nbsp;&nbsp;//只获得变量/对象地址而不获得大小 <br />
<br />
3.void指针赋值给其他类型的指针时都要进行转换 <br />
&nbsp;&nbsp; type *p=(type*)vp; <br />
&nbsp;&nbsp; //转换类型也就是获得指向变量/对象大小 <br />
转:http://icoding.spaces.live.com/blog/cns!209684E38D520BA6!130.entry <br />
<br />
4.void指针不能复引用 <br />
&nbsp;&nbsp;*vp//错误 <br />
&nbsp;&nbsp;因为void指针只知道,指向变量/对象的起始地址 <br />
&nbsp;&nbsp;而不知道指向变量/对象的大小(占几个字节)所以无法正确引用 <br />
<br />
5.void指针不能参与指针运算,除非进行转换 <br />
&nbsp;&nbsp; (type*)vp++; <br />
&nbsp;&nbsp;//vp==vp+sizeof(type)<br />
<br />
<br />
<br />
#include&lt;iostream&gt;<br />
#include&lt;stdlib.h&gt;<br />
#include&lt;string&gt;<br />
using namespace std;<br />
typedef struct tag_st <br />
{ <br />
char id[10];<br />
float fa[2];<br />
}ST; <br />
//我在程序里面这样使用的 <br />
int main()<br />
{<br />
ST * P=(ST *)malloc(sizeof(ST));<br />
strcpy(P-&gt;id,"hello!");<br />
P-&gt;fa[0]=1.1;<br />
P-&gt;fa[1]=2.1;<br />
<br />
ST * Q=(ST *)malloc(sizeof(ST));<br />
strcpy(Q-&gt;id,"world!");<br />
Q-&gt;fa[0]=3.1;<br />
Q-&gt;fa[1]=4.1;<br />
void ** plink=(void **)P;<br />
*((ST *)(plink)) = * Q; //<font style="line-height: 1.3em;" color="#ff0000">plink要先强制转换一下,目的是为了让它先知道要覆盖的大小.</font><wbr><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //<font style="line-height: 1.3em;" color="#ff0000">P的内容竟然给Q的内容覆盖掉了.</font><wbr><br />
cout&lt;&lt;P-&gt;id&lt;&lt;" "&lt;&lt;P-&gt;fa[0]&lt;&lt;" "&lt;&lt;P-&gt;fa[1]&lt;&lt;endl;<br />
return 0;<br />
}<br />
<br />
<br />
<br />
转自：http://www.cppblog.com/dragon/archive/2008/09/02/60760.aspx<br />
</div>
<img src ="http://www.blogjava.net/shiliqiang/aggbug/290353.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shiliqiang/" target="_blank">石头@</a> 2009-08-08 17:09 <a href="http://www.blogjava.net/shiliqiang/articles/290353.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>指针详解</title><link>http://www.blogjava.net/shiliqiang/articles/285866.html</link><dc:creator>石头@</dc:creator><author>石头@</author><pubDate>Tue, 07 Jul 2009 13:21:00 GMT</pubDate><guid>http://www.blogjava.net/shiliqiang/articles/285866.html</guid><wfw:comment>http://www.blogjava.net/shiliqiang/comments/285866.html</wfw:comment><comments>http://www.blogjava.net/shiliqiang/articles/285866.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shiliqiang/comments/commentRss/285866.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shiliqiang/services/trackbacks/285866.html</trackback:ping><description><![CDATA[We have already seen how variables are seen as memory cells that can be
accessed using their identifiers. This way we did not have to care
about the physical location of our data within memory, we simply used
its identifier whenever we wanted to refer to our variable.<br />
<br />
The
memory of your computer can be imagined as a succession of memory
cells, each one of the minimal size that computers manage (one byte).
These single-byte memory cells are numbered in a consecutive way, so
as, within any block of memory, every cell has the same number as the
previous one plus one.<br />
<br />
This way, each cell can be easily located
in the memory because it has a unique address and all the memory cells
follow a successive pattern. For example, if we are looking for cell
1776 we know that it is going to be right between cells 1775 and 1777,
exactly one thousand cells after 776 and exactly one thousand cells
before cell 2776.<br />
<br />
<a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="reference"></a>
<h3>Reference operator (&amp;)</h3>
As soon as we declare a variable, the amount of memory needed is
assigned for it at a specific location in memory (its memory address).
We generally do not actively decide the exact location of the variable
within the panel of cells that we have imagined the memory to be -
Fortunately, that is a task automatically performed by the operating
system during runtime. However, in some cases we may be interested in
knowing the address where our variable is being stored during runtime
in order to operate with relative positions to it.<br />
<br />
The address that locates a variable within memory is what we call a <em>reference</em>
to that variable. This reference to a variable can be obtained by
preceding the identifier of a variable with an ampersand sign (<tt>&amp;</tt>), known as reference operator, and which can be literally translated as "address of". For example: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>ted = &amp;andy;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
This would assign to <tt>ted</tt> the address of variable <tt>andy</tt>, since when preceding the name of the variable <tt>andy</tt> with the reference operator (<tt>&amp;</tt>) we are no longer talking about the content of the variable itself, but about its reference (i.e., its address in memory).<br />
<br />
From now on we are going to assume that <tt>andy</tt> is placed during runtime in the memory address <tt>1776</tt>. This number (<tt>1776</tt>)
is just an arbitrary assumption we are inventing right now in order to
help clarify some concepts in this tutorial, but in reality, we cannot
know before runtime the real value the address of a variable will have
in memory.<br />
<br />
Consider the following code fragment:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>andy = 25;<br />
            fred = andy;<br />
            ted = &amp;andy; </pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
The values contained in each variable after the execution of this, are shown in the following diagram: <br />
<br />
<img src="http://www.cplusplus.com/doc/tutorial/pointers/reference_operator.gif"  alt="" /><br />
<br />
First, we have assigned the value 25 to <tt>andy</tt> (a variable whose address in memory we have assumed to be 1776).<br />
<br />
The second statement copied to <tt>fred</tt> the content of variable <tt>andy</tt> (which is 25). This is a standard assignment operation, as we have done so many times before.<br />
<br />
Finally, the third statement copies to <tt>ted</tt> not the value contained in <tt>andy</tt> but a reference to it (i.e., its address, which we have assumed to be <tt>1776</tt>). The reason is that in this third assignment operation we have preceded the identifier <tt>andy</tt> with the reference operator (<tt>&amp;</tt>), so we were no longer referring to the value of andy but to its reference (its address in memory).<br />
<br />
The variable that stores the reference to another variable (like <tt>ted</tt> in the previous example) is what we call a <em>pointer</em>.
Pointers are a very powerful feature of the C++ language that has many
uses in advanced programming. Farther ahead, we will see how this type
of variable is used and declared.<br />
<br />
<a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="dereference"></a>
<h3>Dereference operator (*)</h3>
<br />
<br />
We
have just seen that a variable which stores a reference to another
variable is called a pointer. Pointers are said to "point to" the
variable whose reference they store.<br />
<br />
Using a pointer we can
directly access the value stored in the variable which it points to. To
do this, we simply have to precede the pointer's identifier with an
asterisk (*), which acts as dereference operator and that can be
literally translated to "value pointed by".<br />
<br />
Therefore, following with the values of the previous example, if we write: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>beth = *ted;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
(that we could read as: "<tt>beth</tt> equal to value pointed by <tt>ted</tt>") <tt>beth</tt> would take the value <tt>25</tt>, since <tt>ted</tt> is <tt>1776</tt>, and the value pointed by 1776 is 25.<br />
<br />
<img src="http://www.cplusplus.com/doc/tutorial/pointers/dereference_operator.gif"  alt="" /><br />
<br />
You must clearly differentiate that the expression <tt>ted</tt> refers to the value <tt>1776</tt>, while <tt>*ted</tt> (with an asterisk <tt>*</tt> preceding the identifier) refers to the value stored at address <tt>1776</tt>, which in this case is <tt>25</tt>.
Notice the difference of including or not including the dereference
operator (I have included an explanatory commentary of how each of
these two expressions could be read): <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>beth = ted;   <span class="comm">// beth equal to ted ( 1776 )</span><br />
            beth = *ted;  <span class="comm">// beth equal to value pointed by ted ( 25 ) </span></pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
Notice the difference between the reference and dereference operators:<br />
<ul>
    <li>&amp; is the reference operator and can be read as "address of"</li>
    <li>* is the dereference operator and can be read as "value pointed by"</li>
</ul>
Thus, they have complementary (or opposite) meanings. A variable referenced with <tt>&amp;</tt> can be dereferenced with <tt>*</tt>.<br />
<br />
Earlier we performed the following two assignment operations:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>andy = 25;<br />
            ted = &amp;andy;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
Right after these two statements, all of the following expressions would give true as result:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>andy == 25<br />
            &amp;andy == 1776<br />
            ted == 1776<br />
            *ted == 25</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
The first expression is quite clear considering that the assignment operation performed on <tt>andy</tt> was <tt>andy=25</tt>. The second one uses the reference operator (<tt>&amp;</tt>), which returns the address of variable <tt>andy</tt>, which we assumed it to have a value of <tt>1776</tt>. The third one is somewhat obvious since the second expression was true and the assignment operation performed on <tt>ted</tt> was <tt>ted=&amp;andy</tt>. The fourth expression uses the dereference operator (<tt>*</tt>) that, as we have just seen, can be read as "value pointed by", and the value pointed by <tt>ted</tt> is indeed <tt>25</tt>.<br />
<br />
So, after all that, you may also infer that for as long as the address pointed by <tt>ted</tt> remains unchanged the following expression will also be true: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>*ted == andy</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
<a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif);" name="declaring_pointers"></a>
<h3>Declaring variables of pointer types</h3>
Due to the ability of a pointer to directly refer to the value that it
points to, it becomes necessary to specify in its declaration which
data type a pointer is going to point to. It is not the same thing to
point to a <tt>char</tt> as to point to an <tt>int</tt> or a <tt>float</tt>.<br />
<br />
The declaration of pointers follows this format:<br />
<br />
<tt>
type * name; <br />
</tt><br />
<br />
where <tt>type</tt>
is the data type of the value that the pointer is intended to point to.
This type is not the type of the pointer itself! but the type of the
data the pointer points to. For example:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">int</span> * number;<br />
            <span class="kw">char</span> * character;<br />
            <span class="kw">float</span> * greatnumber;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
These
are three declarations of pointers. Each one is intended to point to a
different data type, but in fact all of them are pointers and all of
them will occupy the same amount of space in memory (the size in memory
of a pointer depends on the platform where the code is going to run).
Nevertheless, the data to which they point to do not occupy the same
amount of space nor are of the same type: the first one points to an <tt>int</tt>, the second one to a <tt>char</tt> and the last one to a <tt>float</tt>.
Therefore, although these three example variables are all of them
pointers which occupy the same size in memory, they are said to have
different types: <tt>int*</tt>, <tt>char*</tt> and <tt>float*</tt> respectively, depending on the type they point to.<br />
<br />
I want to emphasize that the asterisk sign (<tt>*</tt>)
that we use when declaring a pointer only means that it is a pointer
(it is part of its type compound specifier), and should not be confused
with the dereference operator that we have seen a bit earlier, but
which is also written with an asterisk (<tt>*</tt>). They are simply two different things represented with the same sign.<br />
<br />
Now have a look at this code:<br />
<br />
<table class="codebox">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="comm">// my first pointer</span><br />
            <span class="prep">#include &lt;iostream&gt;</span><br />
            <span class="kw">using</span> <span class="kw">namespace</span> std;<br />
            <br />
            <span class="kw">int</span> main ()<br />
            {<br />
            <span class="kw">int</span> firstvalue, secondvalue;<br />
            <span class="kw">int</span> * mypointer;<br />
            <br />
            mypointer = &amp;firstvalue;<br />
            *mypointer = 10;<br />
            mypointer = &amp;secondvalue;<br />
            *mypointer = 20;<br />
            cout &lt;&lt; <span class="str">"firstvalue is "</span> &lt;&lt; firstvalue &lt;&lt; endl;<br />
            cout &lt;&lt; <span class="str">"secondvalue is "</span> &lt;&lt; secondvalue &lt;&lt; endl;<br />
            <span class="kw">return</span> 0;<br />
            }<br />
            </pre>
            </td>
            <td class="result">
            <pre>firstvalue is 10<br />
            secondvalue is 20</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
Notice that even though we have never directly set a value to either <tt>firstvalue</tt> or <tt>secondvalue</tt>, both end up with a value set indirectly through the use of <tt>mypointer</tt>. This is the procedure:<br />
<br />
First, we have assigned as value of <tt>mypointer</tt> a reference to <tt>firstvalue</tt> using the reference operator (<tt>&amp;</tt>). And then we have assigned the value 10 to the memory location pointed by <tt>mypointer</tt>, that because at this moment is pointing to the memory location of <tt>firstvalue</tt>, this in fact modifies the value of <tt>firstvalue</tt>.<br />
<br />
In
order to demonstrate that a pointer may take several different values
during the same program I have repeated the process with <tt>secondvalue</tt> and that same pointer, <tt>mypointer</tt>.<br />
<br />
Here is an example a little bit more elaborated:<br />
<br />
<table class="codebox">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="comm">// more pointers</span><br />
            <span class="prep">#include &lt;iostream&gt;</span><br />
            <span class="kw">using</span> <span class="kw">namespace</span> std;<br />
            <br />
            <span class="kw">int</span> main ()<br />
            {<br />
            <span class="kw">int</span> firstvalue = 5, secondvalue = 15;<br />
            <span class="kw">int</span> * p1, * p2;<br />
            <br />
            p1 = &amp;firstvalue;  <span class="comm">// p1 = address of firstvalue</span><br />
            p2 = &amp;secondvalue; <span class="comm">// p2 = address of secondvalue</span><br />
            *p1 = 10;          <span class="comm">// value pointed by p1 = 10</span><br />
            *p2 = *p1;         <span class="comm">// value pointed by p2 = value pointed by p1</span><br />
            p1 = p2;           <span class="comm">// p1 = p2 (value of pointer is copied)</span><br />
            *p1 = 20;          <span class="comm">// value pointed by p1 = 20</span><br />
            <br />
            cout &lt;&lt; <span class="str">"firstvalue is "</span> &lt;&lt; firstvalue &lt;&lt; endl;<br />
            cout &lt;&lt; <span class="str">"secondvalue is "</span> &lt;&lt; secondvalue &lt;&lt; endl;<br />
            <span class="kw">return</span> 0;<br />
            }<br />
            </pre>
            </td>
            <td class="result">
            <pre>firstvalue is 10<br />
            secondvalue is 20</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
I have included as a comment on each line how the code can be read: ampersand (<tt>&amp;</tt>) as "address of" and asterisk (<tt>*</tt>) as "value pointed by".<br />
<br />
Notice that there are expressions with pointers <tt>p1</tt> and <tt>p2</tt>, both with and without dereference operator (<tt>*</tt>). The meaning of an expression using the dereference operator (<tt>*</tt>)
is very different from one that does not: When this operator precedes
the pointer name, the expression refers to the value being pointed,
while when a pointer name appears without this operator, it refers to
the value of the pointer itself (i.e. the address of what the pointer
is pointing to).<br />
<br />
Another thing that may call your attention is the line: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">int</span> * p1, * p2;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
This
declares the two pointers used in the previous example. But notice that
there is an asterisk (*) for each pointer, in order for both to have
type <tt>int*</tt> (pointer to <tt>int</tt>).<br />
<br />
Otherwise, the type for the second variable declared in that line would have been <tt>int</tt> (and not <tt>int*</tt>) because of precedence relationships. If we had written:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">int</span> * p1, p2;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
<tt>p1</tt> would indeed have <tt>int*</tt> type, but <tt>p2</tt> would have type <tt>int</tt>
(spaces do not matter at all for this purpose). This is due to operator
precedence rules. But anyway, simply remembering that you have to put
one asterisk per pointer is enough for most pointer users.<br />
<br />
<h3>Pointers and arrays</h3>
The concept of array is very much bound to the one of pointer. In fact,
the identifier of an array is equivalent to the address of its first
element, as a pointer is equivalent to the address of the first element
that it points to, so in fact they are the same concept. For example,
supposing these two declarations:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">int</span> numbers [20];<br />
            <span class="kw">int</span> * p;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
The following assignment operation would be valid: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>p = numbers; </pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
After that, <tt>p</tt> and <tt>numbers</tt> would be equivalent and would have the same properties. The only difference is that we could change the value of pointer <tt>p</tt> by another one, whereas <tt>numbers</tt> will always point to the first of the 20 elements of type <tt>int</tt> with which it was defined. Therefore, unlike <tt>p</tt>, which is an ordinary pointer, <tt>numbers</tt> is an array, and an array can be considered a <em>constant pointer</em>. Therefore, the following allocation would not be valid:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>numbers = p;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
Because <tt>numbers</tt> is an array, so it operates as a constant pointer, and we cannot assign values to constants.<br />
<br />
Due to the characteristics of variables, all expressions that include pointers in the following example are perfectly valid:<br />
<br />
<table class="codebox">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="comm">// more pointers</span><br />
            <span class="prep">#include &lt;iostream&gt;</span><br />
            <span class="kw">using</span> <span class="kw">namespace</span> std;<br />
            <br />
            <span class="kw">int</span> main ()<br />
            {<br />
            <span class="kw">int</span> numbers[5];<br />
            <span class="kw">int</span> * p;<br />
            p = numbers;  *p = 10;<br />
            p++;  *p = 20;<br />
            p = &amp;numbers[2];  *p = 30;<br />
            p = numbers + 3;  *p = 40;<br />
            p = numbers;  *(p+4) = 50;<br />
            <span class="kw">for</span> (<span class="kw">int</span> n=0; n&lt;5; n++)<br />
            cout &lt;&lt; numbers[n] &lt;&lt; <span class="str">", "</span>;<br />
            <span class="kw">return</span> 0;<br />
            }<br />
            </pre>
            </td>
            <td class="result">
            <pre>10, 20, 30, 40, 50, </pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
In the chapter about arrays we used brackets (<tt>[]</tt>)
several times in order to specify the index of an element of the array
to which we wanted to refer. Well, these bracket sign operators <tt>[]</tt> are also a <u>dereference</u> operator known as <em>offset operator</em>. They dereference the variable they follow just as <tt>*</tt> does, but they also add the number between brackets to the address being dereferenced. For example:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>a[5] = 0;       <span class="comm">// a [offset of 5] = 0</span><br />
            *(a+5) = 0;     <span class="comm">// pointed by (a+5) = 0 </span></pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
These two expressions are equivalent and valid both if <tt>a</tt> is a pointer or if <tt>a</tt> is an array.<br />
<br />
<h3>Pointer initialization</h3>
When declaring pointers we may want to explicitly specify which variable we want them to point to:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">int</span> number;<br />
            <span class="kw">int</span> *tommy = &amp;number;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
The behavior of this code is equivalent to:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">int</span> number;<br />
            <span class="kw">int</span> *tommy;<br />
            tommy = &amp;number;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
When a pointer initialization takes place we are always assigning the reference value to where the pointer points (<tt>tommy</tt>), never the value being pointed (<tt>*tommy</tt>). You must consider that at the moment of declaring a pointer, the asterisk (<tt>*</tt>)
indicates only that it is a pointer, it is not the dereference operator
(although both use the same sign: *). Remember, they are two different
functions of one sign. Thus, we must take care not to confuse the
previous code with: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">int</span> number;<br />
            <span class="kw">int</span> *tommy;<br />
            *tommy = &amp;number;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
that is incorrect, and anyway would not have much sense in this case if you think about it.<br />
<br />
As
in the case of arrays, the compiler allows the special case that we
want to initialize the content at which the pointer points with
constants at the same moment the pointer is declared:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">char</span> * terry = <span class="str">"hello"</span>; </pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
In this case, memory space is reserved to contain <tt>"hello"</tt> and then a pointer to the first character of this memory block is assigned to <tt>terry</tt>. If we imagine that <tt>"hello"</tt> is stored at the memory locations that start at addresses 1702, we can represent the previous declaration as:<br />
<br />
<img src="http://www.cplusplus.com/doc/tutorial/pointers/pointer_assignment.gif"  alt="" /><br />
<br />
It is important to indicate that <tt>terry</tt> contains the value 1702, and not <tt>'h'</tt> nor <tt>"hello"</tt>, although 1702 indeed is the address of both of these.<br />
<br />
The pointer <tt>terry</tt>
points to a sequence of characters and can be read as if it was an
array (remember that an array is just like a constant pointer). For
example, we can access the fifth element of the array with any of these
two expression:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>*(terry+4)<br />
            terry[4]</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
Both expressions have a value of <tt>'o'</tt> (the fifth element of the array).<br />
<br />
<h3>Pointer arithmetics</h3>
<br />
<br />
To
conduct arithmetical operations on pointers is a little different than
to conduct them on regular integer data types. To begin with, only
addition and subtraction operations are allowed to be conducted with
them, the others make no sense in the world of pointers. But both
addition and subtraction have a different behavior with pointers
according to the size of the data type to which they point.<br />
<br />
When
we saw the different fundamental data types, we saw that some occupy
more or less space than others in the memory. For example, let's assume
that in a given compiler for a specific machine, <tt>char</tt> takes 1 byte, <tt>short</tt> takes 2 bytes and <tt>long</tt> takes 4.<br />
<br />
Suppose that we define three pointers in this compiler: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">char</span> *mychar;<br />
            <span class="kw">short</span> *myshort;<br />
            <span class="kw">long</span> *mylong;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
and that we know that they point to memory locations <tt>1000</tt>, <tt>2000</tt> and <tt>3000</tt> respectively. <br />
<br />
So if we write: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>mychar++;<br />
            myshort++;<br />
            mylong++;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
<tt>mychar</tt>, as you may expect, would contain the value <tt>1001</tt>. But not so obviously, <tt>myshort</tt> would contain the value <tt>2002</tt>, and <tt>mylong</tt> would contain <tt>3004</tt>,
even though they have each been increased only once. The reason is that
when adding one to a pointer we are making it to point to the following
element of the same type with which it has been defined, and therefore
the size in bytes of the type pointed is added to the pointer.<br />
<br />
<img src="http://www.cplusplus.com/doc/tutorial/pointers/pointer_arithmetics.gif"  alt="" /><br />
<br />
This is applicable both when adding and subtracting any number to a pointer. It would happen exactly the same if we write: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>mychar = mychar + 1;<br />
            myshort = myshort + 1;<br />
            mylong = mylong + 1;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
Both the increase (<tt>++</tt>) and decrease (<tt>--</tt>) operators have greater operator precedence than the dereference operator (<tt>*</tt>),
but both have a special behavior when used as suffix (the expression is
evaluated with the value it had before being increased). Therefore, the
following expression may lead to confusion: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>*p++</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
Because <tt>++</tt> has greater precedence than <tt>*</tt>, this expression is equivalent to <tt>*(p++)</tt>.
Therefore, what it does is to increase the value of p (so it now points
to the next element), but because ++ is used as postfix the whole
expression is evaluated as the value pointed by the original reference
(the address the pointer pointed to before being increased).<br />
<br />
Notice the difference with:<br />
<br />
<tt>(*p)++</tt><br />
<br />
Here, the expression would have been evaluated as the value pointed by <tt>p</tt> increased by one. The value of <tt>p</tt> (the pointer itself) would not be modified (what is being modified is what it is being pointed to by this pointer).<br />
<br />
If we write:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>*p++ = *q++; </pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
Because <tt>++</tt> has a higher precedence than <tt>*</tt>, both <tt>p</tt> and <tt>q</tt> are increased, but because both increase operators (<tt>++</tt>) are used as postfix and not prefix, the value assigned to <tt>*p</tt> is <tt>*q</tt> <u>before</u> both <tt>p</tt> and <tt>q</tt> are increased. And then both are increased. It would be roughly equivalent to:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre>*p = *q;<br />
            ++p;<br />
            ++q;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
Like always, I recommend you to use parentheses <tt>()</tt> in order to avoid unexpected results and to give more legibility to the code.<br />
<br />
<h3>Pointers to pointers</h3>
C++ allows the use of pointers that point to pointers, that these, in
its turn, point to data (or even to other pointers). In order to do
that, we only need to add an asterisk (<tt>*</tt>) for each level of reference in their declarations:<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">char</span> a;<br />
            <span class="kw">char</span> * b;<br />
            <span class="kw">char</span> ** c;<br />
            a = <span class="str">'z'</span>;<br />
            b = &amp;a;<br />
            c = &amp;b;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
This, supposing the randomly chosen memory locations for each variable of <tt>7230</tt>, <tt>8092</tt> and <tt>10502</tt>, could be represented as:<br />
<br />
<img src="http://www.cplusplus.com/doc/tutorial/pointers/pointer_to_pointer.gif"  alt="" /><br />
<br />
The value of each variable is written inside each cell; under the cells are their respective addresses in memory.<br />
<br />
The new thing in this example is variable <tt>c</tt>, which can be used in three different levels of indirection, each one of them would correspond to a different value:<br />
<br />
<ul>
    <li><tt>c</tt> has type <tt>char**</tt> and a value of <tt>8092</tt></li>
    <li><tt>*c</tt> has type <tt>char*</tt> and a value of <tt>7230</tt></li>
    <li><tt>**c</tt> has type <tt>char</tt> and a value of <tt>'z'</tt></li>
</ul>
<br />
<br />
<h3>void pointers</h3>
The <tt>void</tt> type of pointer is a special type of pointer. In C++, <tt>void</tt>
represents the absence of type, so void pointers are pointers that
point to a value that has no type (and thus also an undetermined length
and undetermined dereference properties).<br />
<br />
This allows void
pointers to point to any data type, from an integer value or a float to
a string of characters. But in exchange they have a great limitation:
the data pointed by them cannot be directly dereferenced (which is
logical, since we have no type to dereference to), and for that reason
we will always have to cast the address in the void pointer to some
other pointer type that points to a concrete data type before
dereferencing it.<br />
<br />
One of its uses may be to pass generic parameters to a function: <br />
<br />
<table class="codebox">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="comm">// increaser</span><br />
            <span class="prep">#include &lt;iostream&gt;</span><br />
            <span class="kw">using</span> <span class="kw">namespace</span> std;<br />
            <br />
            <span class="kw">void</span> increase (<span class="kw">void</span>* data, <span class="kw">int</span> psize)<br />
            {<br />
            <span class="kw">if</span> ( psize == <span class="kw">sizeof</span>(<span class="kw">char</span>) )<br />
            { <span class="kw">char</span>* pchar; pchar=(<span class="kw">char</span>*)data; ++(*pchar); }<br />
            <span class="kw">else</span> <span class="kw">if</span> (psize == <span class="kw">sizeof</span>(<span class="kw">int</span>) )<br />
            { <span class="kw">int</span>* pint; pint=(<span class="kw">int</span>*)data; ++(*pint); }<br />
            }<br />
            <br />
            <span class="kw">int</span> main ()<br />
            {<br />
            <span class="kw">char</span> a = <span class="str">'x'</span>;<br />
            <span class="kw">int</span> b = 1602;<br />
            increase (&amp;a,<span class="kw">sizeof</span>(a));<br />
            increase (&amp;b,<span class="kw">sizeof</span>(b));<br />
            cout &lt;&lt; a &lt;&lt; <span class="str">", "</span> &lt;&lt; b &lt;&lt; endl;<br />
            <span class="kw">return</span> 0;<br />
            }<br />
            </pre>
            </td>
            <td class="result">
            <pre>y, 1603</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
<tt>sizeof</tt>
is an operator integrated in the C++ language that returns the size in
bytes of its parameter. For non-dynamic data types this value is a
constant. Therefore, for example, <tt>sizeof(char)</tt> is <tt>1</tt>, because <tt>char</tt> type is one byte long. <br />
<br />
<h3>Null pointer</h3>
A null pointer is a regular pointer of any pointer type which has a
special value that indicates that it is not pointing to any valid
reference or memory address. This value is the result of type-casting
the integer value zero to any pointer type.<br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">int</span> * p;<br />
            p = 0;     <span class="comm">// p has a null pointer value </span></pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
Do
not confuse null pointers with void pointers. A null pointer is a value
that any pointer may take to represent that it is pointing to
"nowhere", while a void pointer is a special type of pointer that can
point to somewhere without a specific type. One refers to the value
stored in the pointer itself and the other to the type of data it
points to.<br />
<br />
<h3>Pointers to functions</h3>
C++ allows operations with pointers to functions. The typical use of
this is for passing a function as an argument to another function,
since these cannot be passed dereferenced. In order to declare a
pointer to a function we have to declare it like the prototype of the
function except that the name of the function is enclosed between
parentheses <tt>()</tt> and an asterisk (<tt>*</tt>) is inserted before the name:<br />
<br />
<table class="codebox">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="comm">// pointer to functions</span><br />
            <span class="prep">#include &lt;iostream&gt;</span><br />
            <span class="kw">using</span> <span class="kw">namespace</span> std;<br />
            <br />
            <span class="kw">int</span> addition (<span class="kw">int</span> a, <span class="kw">int</span> b)<br />
            { <span class="kw">return</span> (a+b); }<br />
            <br />
            <span class="kw">int</span> subtraction (<span class="kw">int</span> a, <span class="kw">int</span> b)<br />
            { <span class="kw">return</span> (a-b); }<br />
            <br />
            <span class="kw">int</span> operation (<span class="kw">int</span> x, <span class="kw">int</span> y, <span class="kw">int</span> (*functocall)(<span class="kw">int</span>,<span class="kw">int</span>))<br />
            {<br />
            <span class="kw">int</span> g;<br />
            g = (*functocall)(x,y);<br />
            <span class="kw">return</span> (g);<br />
            }<br />
            <br />
            <span class="kw">int</span> main ()<br />
            {<br />
            <span class="kw">int</span> m,n;<br />
            <span class="kw">int</span> (*minus)(<span class="kw">int</span>,<span class="kw">int</span>) = subtraction;<br />
            <br />
            m = operation (7, 5, addition);<br />
            n = operation (20, m, minus);<br />
            cout &lt;&lt;n;<br />
            <span class="kw">return</span> 0;<br />
            }<br />
            </pre>
            </td>
            <td class="result">
            <pre>8</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<br />
In the example, <tt>minus</tt> is a pointer to a function that has two parameters of type <tt>int</tt>. It is immediately assigned to point to the function <tt>subtraction</tt>, all in a single line: <br />
<br />
<table class="snippet">
    <tbody>
        <tr>
            <td class="code">
            <pre><span class="kw">int</span> (* minus)(<span class="kw">int</span>,<span class="kw">int</span>) = subtraction;</pre>
            </td>
        </tr>
    </tbody>
</table>
<p>&nbsp;</p>
<img src ="http://www.blogjava.net/shiliqiang/aggbug/285866.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shiliqiang/" target="_blank">石头@</a> 2009-07-07 21:21 <a href="http://www.blogjava.net/shiliqiang/articles/285866.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>