﻿<?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>C++博客-DWZ-文章分类-C语言</title><link>http://www.cppblog.com/dwz-1987/category/6756.html</link><description>MAKI</description><language>zh-cn</language><lastBuildDate>Mon, 19 May 2008 13:26:40 GMT</lastBuildDate><pubDate>Mon, 19 May 2008 13:26:40 GMT</pubDate><ttl>60</ttl><item><title>函数指针和指针函数</title><link>http://www.cppblog.com/dwz-1987/articles/48350.html</link><dc:creator>刘冬清</dc:creator><author>刘冬清</author><pubDate>Mon, 28 Apr 2008 08:10:00 GMT</pubDate><guid>http://www.cppblog.com/dwz-1987/articles/48350.html</guid><wfw:comment>http://www.cppblog.com/dwz-1987/comments/48350.html</wfw:comment><comments>http://www.cppblog.com/dwz-1987/articles/48350.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/dwz-1987/comments/commentRss/48350.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dwz-1987/services/trackbacks/48350.html</trackback:ping><description><![CDATA[<p><strong>函数指针</strong><br><br>在程序运行中，函数代码是程序的算法指令部分，它们和数组一样也占用存储空间，都有相应的地址。可以使用指针变量指向数组的首地址，也可以使用指针变量指向函数代码的首地址，指向函数代码首地址的指针变量称为函数指针。<br><br>1．函数指针定义<br><br>函数类型 （*指针变量名）(形参列表)；<br><br>&#8220;函数类型&#8221;说明函数的返回类型，由于&#8220;()&#8221;的优先级高于&#8220;*&#8221;,所以指针变量名外的括号必不可少，后面的&#8220;形参列表&#8221;表示指针变量指向的函数所带的参数列表。<br><br>例如：<br><br>int (*f)(int x);<br><br>double (*ptr)(double x);<br><br>在定义函数指针时请注意：<br>&nbsp;&nbsp;&nbsp; <br>函数指针和它指向的函数的参数个数和类型都应该是—致的；<br><br>函数指针的类型和函数的返回值类型也必须是一致的。<br><br>2．函数指针的赋值<br><br>函数名和数组名一样代表了函数代码的首地址，因此在赋值时，直接将函数指针指向函数名就行了。<br><br>例如，<br><br>int func(int x);&nbsp;&nbsp; /* 声明一个函数 */<br><br>int (*f) (int x);&nbsp;&nbsp;&nbsp; /* 声明一个函数指针 */<br><br>f=func;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /* 将func函数的首地址赋给指针f */<br><br>赋值时函数func不带括号，也不带参数，由于func代表函数的首地址，因此经过赋值以后，指针f就指向函数func(x)的代码的首地址。<br><br>3．通过函数指针调用函数<br><br>函数指针是通过函数名及有关参数进行调用的。<br><br>与其他指针变量相类似，如果指针变量pi是指向某整型变量i的指针，则*p等于它所指的变量i；如果pf是指向某浮点型变量f的指针，则*pf就等价于它所指的变量f。同样地，*f是指向函数func(x)的指针，则*f就代表它所指向的函数func。所以在执行了f=func;之后，(*f)和func代表同一函数。<br><br>由于函数指针指向存储区中的某个函数，因此可以通过函数指针调用相应的函数。现在我们就讨论如何用函数指针调用函数，它应执行下面三步：<br><br>首先，要说明函数指针变量。<br><br>例如：int (*f)(int x);<br><br>其次，要对函数指针变量赋值。<br><br>例如： f=func;&nbsp;&nbsp;&nbsp; (func(x)必须先要有定义)<br><br>最后，要用 (*指针变量)(参数表);调用函数。<br><br>例如：&nbsp;&nbsp;&nbsp; (*f)(x);(x必须先赋值)<br><br><br>【例】任意输入n个数，找出其中最大数，并且输出最大数值。<br><br>main()<br><br>{<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int f();<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int i，a，b;<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int (*p)();&nbsp;&nbsp;&nbsp; /* 定义函数指针 */<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; scanf("%d"，&amp;a);<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; p=f;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /* 给函数指针p赋值，使它指向函数f */<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for(i＝1;i&lt;9;i++)<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; scanf("%d"，&amp;b);<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; a＝(*p)(a，b);&nbsp;&nbsp;&nbsp; /* 通过指针p调用函数f */<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("The Max Number is:%d"，a)<br><br>}<br><br><br>f(int x，int y)<br><br>{<br><br>&nbsp;&nbsp;&nbsp; int z;<br><br>&nbsp;&nbsp;&nbsp; z＝(x&gt;y)?x:y;<br><br>&nbsp;&nbsp;&nbsp; return(z);<br><br>}<br><br>运行结果为：<br><br>343 -45 4389 4235 1 -534 988 555 789↙<br><br>The Max Number is：4389<br><br><br><strong>指针函数</strong><br><br>一个函数不仅可以带回一个整型数据的值，字符类型值和实型类型的值，还可以带回指针类型的数据，使其指向某个地址单元。<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 返回指针的函数，一般定义格式为：<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 类型标识符&nbsp;&nbsp;&nbsp; *函数名(参数表)<br><br>int *f(x，y);<br><br>其中x，y是形式参数，f是函数名，调用后返回一个指向整型数据的地址指针。f(x，y)是函数，其值是指针。<br><br>如：char *ch();表示的就是一个返回字符型指针的函数，请看下面的例题：<br><br>【例】将字符串1(str1)复制到字符串2(str2)，并输出字符串2.<br><br>#include "stdio.h"<br><br>main()<br><br>{<br><br>&nbsp;&nbsp;&nbsp; char *ch(char *，char *);<br><br>&nbsp;&nbsp;&nbsp; char str1[]="I am glad to meet you!";<br><br>&nbsp;&nbsp;&nbsp; char str2[]="Welcom to study C!";<br><br>&nbsp;&nbsp;&nbsp; printf("%s"，ch(str1，str2));<br><br>}<br><br>char *ch(char *str1，char *str2)<br><br>{<br><br>&nbsp;&nbsp;&nbsp; int i;<br><br>&nbsp;&nbsp;&nbsp; char *p;<br><br>&nbsp;&nbsp;&nbsp; p=str2<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; if(*str2==NULL) exit(-1);<br><br>&nbsp;&nbsp;&nbsp; do<br><br>&nbsp;&nbsp;&nbsp; { <br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; *str2=*str1;<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; str1++;<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; str2++;<br><br>&nbsp;&nbsp;&nbsp; }while(*str1!=NULL);<br><br>&nbsp;&nbsp;&nbsp; return(p);<br><br>}</p>
<p>&#160;</p>
<p>通过分析可得</p>
<p>函数指针是一个指向函数的指针，而指针函数只是说明他是一个返回值为指针的函数，</p>
<p>函数指针可以用来指向一个函数。</p>
<img src ="http://www.cppblog.com/dwz-1987/aggbug/48350.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dwz-1987/" target="_blank">刘冬清</a> 2008-04-28 16:10 <a href="http://www.cppblog.com/dwz-1987/articles/48350.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C语言中 值传递,地址传递 和 printf的问题</title><link>http://www.cppblog.com/dwz-1987/articles/47735.html</link><dc:creator>刘冬清</dc:creator><author>刘冬清</author><pubDate>Mon, 21 Apr 2008 08:05:00 GMT</pubDate><guid>http://www.cppblog.com/dwz-1987/articles/47735.html</guid><wfw:comment>http://www.cppblog.com/dwz-1987/comments/47735.html</wfw:comment><comments>http://www.cppblog.com/dwz-1987/articles/47735.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/dwz-1987/comments/commentRss/47735.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dwz-1987/services/trackbacks/47735.html</trackback:ping><description><![CDATA[<p>#include "stdio.h"</p>
<p>int change_by_value(int x)<br>{<br>&nbsp;x=x+1;<br>&nbsp;return x;<br>}</p>
<p>int change_by_address(int* y)<br>{<br>&nbsp;(*y)++;<br>&nbsp;return *y;<br>}</p>
<p>void main()<br>{<br>&nbsp;int a=10;<br>&nbsp;printf("a=%d\n",a);<br>&nbsp;printf("change_by_value=%d\n",change_by_value(a));<br>&nbsp;printf("a=%d\n",a);<br>&nbsp;printf("change_by_address=%d\n",change_by_address(&amp;a));<br>&nbsp;printf("a=%d\n",a);<br>//&nbsp;printf("a=%d\nchange_by_value=%d\na=%d\nchange_by_address=%d\na=%d\n",a,change_by_value(a),a,change_by_address(&amp;a),a);<br>}<br>&nbsp;把上面几个printf的内容放在一起,出现了不同的值,很奇怪~~~ </p>
<img src ="http://www.cppblog.com/dwz-1987/aggbug/47735.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dwz-1987/" target="_blank">刘冬清</a> 2008-04-21 16:05 <a href="http://www.cppblog.com/dwz-1987/articles/47735.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态内存分配与释放</title><link>http://www.cppblog.com/dwz-1987/articles/47075.html</link><dc:creator>刘冬清</dc:creator><author>刘冬清</author><pubDate>Mon, 14 Apr 2008 13:25:00 GMT</pubDate><guid>http://www.cppblog.com/dwz-1987/articles/47075.html</guid><wfw:comment>http://www.cppblog.com/dwz-1987/comments/47075.html</wfw:comment><comments>http://www.cppblog.com/dwz-1987/articles/47075.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/dwz-1987/comments/commentRss/47075.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dwz-1987/services/trackbacks/47075.html</trackback:ping><description><![CDATA[
		<p>
				<font size="2">如何实现动态内存分配及其管理<br />要实现根据程序的需要动态分配存储空间，就必须用到以下几个函数：<br /></font>
				<font size="3">
						<strong>1、malloc函数<br /></strong>
						<font face="宋体">库函数 </font>
						<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">
								<b>malloc</b>()</span> <br /></font>
				<font size="3">
						<nobr> <span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">用法：</span><span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">void *malloc(unsigned size)</span></nobr>
						<br />
				</font>
				<font size="3">
						<nobr>
								<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">
										<strong>功能</strong>：在内存的动态存储区分配１个长度为</span>
								<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">size</span>
								<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">的连续空间。</span> </nobr>
						<font size="2">其作用是在内存的动态存储区中分配一个长度为size的连续空间。其参数是一个无符号整形数，返回值是一个指向所分配的连续存储域的起始地址的指针。还有一点必须注意的是，当函数未能成功分配存储空间（如内存不足）就会返回一个NULL指针。所以在调用该函数时应该检测返回值是否为NULL并执行相应的操作。</font>
						<br />
				</font>
				<font size="3">
						<nobr>
								<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">
										<strong>返回值</strong>：申请成功，则返回新分配内存块的起始地址；否则，返回</span>
								<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">NULL</span>
								<font face="宋体">。<br /></font>
						</nobr>
				</font>
				<font size="3">
						<nobr>
								<strong> </strong>
								<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">
										<strong>函数原型</strong>：</span>
								<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">alloc.h</span>
								<span lang="EN-US" style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">，</span>
								<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">stdlib.h</span>
								<span lang="EN-US" style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">。<br /></span>
						</nobr>
				</font>
				<font size="3">
						<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">malloc()</span>
						<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">函数的返回值是一个无类型指针，其特点是可以指向任何类型的数据。但在</span>
						<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">
								<b>
										<i>
												<u>实际使用</u>
										</i>
								</b>
						</span>
						<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">
								<b>
										<i>
												<u>malloc()</u>
										</i>
								</b>
						</span>
						<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">
								<b>
										<i>
												<u>函数时，必须将其返回值强制转换成被赋值指针变量的数据类型</u>
										</i>
								</b>，以免出错。</span>
				</font>
				<nobr>
						<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">
								<br />
								<font size="2">
										<br />
										<strong>运算符</strong>
								</font>
						</span>
						<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">
								<strong>
										<font size="3">sizeof</font>
										<br />
								</strong>
						</span>
				</nobr>
				<font size="3">
						<nobr>
								<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">
										<strong>格式</strong>：</span>
								<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">sizeof(</span>
								<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">变量名／类型名</span>
								<span style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">)</span>
								<br />
						</nobr>
				</font>
				<font size="2">
						<font size="3">
								<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">
										<strong>功能</strong>：求变量／类型占用的内存字节数（正整数）。例如，在</span>
								<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">IBM-PC</span>
								<span style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'">机上，</span>
								<span lang="EN-US" style="FONT-SIZE: 83%; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">sizeof(int)=2</span>
								<span lang="EN-US" style="FONT-SIZE: 83%; FONT-FAMILY: 宋体; mso-fareast-font-family: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">。</span>
						</font>
				</font>
				<br />
				<font size="2">
						<br />下例是一个动态分配的程序：<br />#include &lt;stdio.h&gt;<br />//包含动态内存分配函数的头文件<br />#include &lt;malloc.h&gt;<br />#include "stdlib.h" <br />void main()<br />{<br />//count是一个计数器，array是一个整型指针，也可以理解为指向一个整型数组的首地址<br />int count,*array; <br />//给指针array分配相应类型的存储空间<br />if((array=(int *)malloc(10*sizeof(int)))==NULL)<br />{<br /> printf("不能成功分配存储空间。");<br /> exit(1);<br />}<br />//给数组赋值<br />for(count=0;count&lt;10;count++) <br /> array[count]=count;<br />//打印数组元素<br />for(count=0;count&lt;10;count++) <br /> printf("%2d\n",array[count]);<br />//释放array所指向的空间<br />free(array);<br />}<br /></font>
		</p>
		<p>
				<font size="2">
						<br />上例中动态分配了10个整型存储区域，然后进行赋值并打印。例中if((array=(int *)malloc(10*sizeof(int))) ==NULL)语句可以分为以下几步：<br />1）分配10个整型的连续存储空间，并返回一个指向其起始地址的整型指针<br />2）把此整型指针地址赋给array<br />3）检测返回值是否为NULL<br /><font size="3"><br /><strong>2、free函数</strong></font><br />由于内存区域总是有限的，不能不限制地分配下去，而且一个程序要尽量节省资源，所以当所分配的内存区域不用时，就要释放它，以便其它的变量或者程序使用。这时我们就要用到free函数。<br />其函数原型是：<br /><strong>void free(void *p)</strong><br />作用是释放指针p所指向的内存区。<br />其参数p必须是先前调用malloc函数或calloc函数（另一个动态分配存储区域的函数）时返回的指针。给free函数传递其它的值很可能造成死机或其它灾难性的后果。<br />注意：这里重要的是指针的值，而不是用来申请动态内存的指针本身。例：<br />int *p1,*p2;<br />p1=malloc(10*sizeof(int));<br />p2=p1;<br />……<br />free(p2) /*或者free(p1)*/<br />malloc返回值赋给p1，又把p1的值赋给p2，所以此时p1，p2都可作为free函数的参数。<br />malloc函数是对存储区域进行分配的。<br />free函数是释放已经不用的内存区域的。<br />所以由这两个函数就可以实现对内存区域进行动态分配并进行简单的管理了。</font>
		</p>
<img src ="http://www.cppblog.com/dwz-1987/aggbug/47075.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dwz-1987/" target="_blank">刘冬清</a> 2008-04-14 21:25 <a href="http://www.cppblog.com/dwz-1987/articles/47075.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>