<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]为何数组的首地址当指针传入函数时，得传入数组的长度之sizeof函数，sizeof(数组名)和sizeof(指针)的区别，及fwrite里的第二第三参数的用法。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Thu, 22 Feb 2018 01:02:03 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景:为何数组的首地址当指针传入函数时，得传入数组的长度？在函数体内的其实是通过数组名初始化的指针形参，故不能在函数中通过 sizeof(指针形参)/sizeof(数组元素类型) 来计算数组长度。所以一般将数组名作为形参传入函数时，也会同时传递一个数组长度的参数。为何指针长度是8位，而不是4位？因为系统是64位的（8字节，64位系统）。<br/><br/>数组名一旦传入函数，再用sizeof(数组名，也就是首地址)，其得出的值变为8了，不再是1024了，实践如下：<br/>vi fwrite.c<br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int returnArrPointerLen(char * bufferArrPointer)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;// 这个bufferArrPointer尽管传入的是数组的首地址，但是它的sizeof(bufferArrPointer)不是1024，而是8了，所以得入数组的长度值。
&nbsp;&nbsp;&nbsp;&nbsp;return sizeof(bufferArrPointer);
&#125;
int main()&#123;
&nbsp;&nbsp;&nbsp;&nbsp;char *buf;
&nbsp;&nbsp;&nbsp;&nbsp;char buffer[1024];
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;1)直接在变量申明段里访问数组长度值：sizeof的数组名buffer=%d&#92;n&quot;,sizeof(buffer));
&nbsp;&nbsp;&nbsp;&nbsp;int recvlen;
&nbsp;&nbsp;&nbsp;&nbsp;strcpy(buffer,&quot;jackTestWriteFile&#039;s function named fwrite。&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;2)!!returnArrPointerLen函数返回buffer数组首地址到函数里后长度值就不等于1024了,等于：%d&#92;n&quot;,returnArrPointerLen(buffer));
&nbsp;&nbsp;&nbsp;&nbsp;buf = buffer;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;3)returnArrPointerLen函数传入指向数组buffer首地址返回buffer长度和申明数组长度不一样等于%d&#92;n&quot;,returnArrPointerLen(buf));
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;4)sizeof buf=%d&#92;n&quot;,sizeof(buf));
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;5)strlen buf=%d&#92;n&quot;,strlen(buf));
&nbsp;&nbsp;&nbsp;&nbsp;buf = buf+10;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;6)sizeof buf=%d&#92;n&quot;,sizeof(buf));
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;7)strlen buf=%d&#92;n&quot;,strlen(buf));
&nbsp;&nbsp;&nbsp;&nbsp;recvlen=strlen(buffer);
&nbsp;&nbsp;&nbsp;&nbsp;FILE *dst;
&nbsp;&nbsp;&nbsp;&nbsp;dst=fopen(&quot;/tmp/out.dat&quot;,&quot;a+&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;if(dst==NULL)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;cant&#039;to open destination file&#92;n&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;fwrite(buf,recvlen,1,dst);
&nbsp;&nbsp;&nbsp;&nbsp;fclose(dst);
&#125;
</textarea><br/>#./fwrite <br/>1)直接在变量申明段里访问数组长度值：sizeof的数组名buffer=1024<br/>2)!!returnArrPointerLen函数返回buffer数组首地址到函数里后长度值就不等于1024了,等于：8<br/>3)returnArrPointerLen函数传入指向数组buffer首地址返回buffer长度和申明数组长度不一样等于8<br/>4)sizeof buf=8<br/>5)strlen buf=44<br/>6)sizeof buf=8<br/>7)strlen buf=34<br/><br/>总之，一旦将数组首地址传入函数，那么其长度就变成了指针长度了，而32位系统和64位系统指针分别对应的是4和8：<br/><textarea name="code" class="php" rows="15" cols="100">
#include &quot;stdio.h&quot;

int main(void)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;int number[5] = &#123;1,2,3,4,5&#125;;
&nbsp;&nbsp;&nbsp;&nbsp;int index = 0;
&nbsp;&nbsp;&nbsp;&nbsp;int *p = &amp;number[0];
&nbsp;&nbsp;&nbsp;&nbsp;int count = sizeof(number) / sizeof(number[0]);//得出了5，但是这个如果放到函数里，也就变成指针大小了。参考：https://zhuanlan.zhihu.com/p/24965911

&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;1)指向数组首地址，数组的长度是: %d&#92;n&quot;, sizeof(number));//等于20，于是想除以4，于是用： sizeof(number) / sizeof(number[0]
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;2)用指针指向数组的首地址长度是: %d&#92;n&quot;, sizeof(p));
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;3)试图求下数组第一个键值的空间大小sizeof(number)=%d&#92;n&quot;,sizeof(number[0]));
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;4)再试图通过sizeof(number)/sizeof(number[0]得出5? %d&#92;n&quot;, count);


&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;数组的元素分别是:&#92;n&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;//for(index = 0;index &lt; sizeof(p); index++)
&nbsp;&nbsp;&nbsp;&nbsp;for(index = 0;index &lt; count; index++)//此路不通
&nbsp;&nbsp;&nbsp;&nbsp;//for(index = 0;index &lt; sizeof(number); index++)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%d &quot;, number[index]);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&#92;n&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&#125;
</textarea><br/>#./sizeofarr <br/>1)指向数组首地址，数组的长度是: 20<br/>2)用指针指向数组的首地址长度是: 8<br/>3)试图求下数组第一个键值的空间大小sizeof(number)=4<br/>4)再试图通过sizeof(number)/sizeof(number[0]得出5? 5<br/>数组的元素分别是:<br/>1 2 3 4 5 <br/><br/><br/>在c/c++中我们一般都用 sizeof(数组名)/sizeof(数组元素类型) 来计算数组的长度，其中 sizeof(数组名) 计算的是数组占用的存储大小。同时，一般我们认为数组名和指针就是一回事，最近写程序时将数组名作为指针形参传入函数，想在函数中用 sizeof(指针形参)/sizeof(数组元素类型) 来计算数组长度时却出了错，最后发现原来是sizeof(数组名)和sizeof(指针)的区别导致的，于是写了段代码测试了一下：<br/><br/><textarea name="code" class="php" rows="15" cols="100">
#include&lt;iostream&gt;&nbsp;&nbsp;

void fun(int *E)&nbsp;&nbsp;
&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;std::cout&lt;&lt;&quot;函数指针形参：&quot;&lt;&lt;sizeof(E)&lt;&lt;std::endl;&nbsp;&nbsp;
&#125;&nbsp;&nbsp;
int main()&nbsp;&nbsp;
&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;int A[10];&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;int *B = new int[10]; 
&nbsp;&nbsp;&nbsp;&nbsp;//int *&amp;C = A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//会报错
&nbsp;&nbsp;&nbsp;&nbsp;int *&amp;D = B;
&nbsp;&nbsp;&nbsp;&nbsp;std::cout&lt;&lt;&quot;数组名：&quot;&lt;&lt;sizeof(A)&lt;&lt;std::endl;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;std::cout&lt;&lt;&quot;指针：&quot;&lt;&lt;sizeof(B)&lt;&lt;std::endl;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;std::cout&lt;&lt;&quot;指针引用：&quot;&lt;&lt;sizeof(D)&lt;&lt;std::endl;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;fun(A);&nbsp;&nbsp;
&#125; 
</textarea><br/><br/>输出结果：<br/><br/>数组名：40 <br/>指针：8 <br/>指针引用：8 <br/>函数指针形参：8<br/><br/>可以看到，当sizeof的参数是数组名时，计算的是整个数组的存储大小；当sizeof的参数是指针时，计算的是指针的大小（8字节，64位系统）。而且，可以定义对指针的引用，但却不能用数组名来作为指针引用的右值，可见数组名和指针还是有区别的。同时，将数组名作为实参传入函数时，因为形参是指针，所以在函数体内的其实是通过数组名初始化的指针形参，故不能在函数中通过 sizeof(指针形参)/sizeof(数组元素类型) 来计算数组长度。所以一般将数组名作为形参传入函数时，也会同时传递一个数组长度的参数。<br/><br/>来自：http://blog.csdn.net/u012707739/article/details/75732159<br/><br/><br/>C语言fwrite怎么写入文件？<br/>==================来自：https://zhidao.baidu.com/question/1893763120275364500.html==================<br/>我设了一个字符串char rgb[3]; 然后用fwrite(&amp;rgb,3,1,file)和fwrite(&amp;rgb,1,3,file)写入一个bmp文件 这两个得到了相同的东西 而fwrite(&amp;rgb,3,2,file)得到的是rgb的三个数据+三个CC(204) 请问fwrite是怎么读取rgb的数据的？ 是不是按照首地址++? fwrite(&amp;rgb,3,2,file)里前三个数读完了然后想要读取rgb[3] [4]和[5]但是读取的是乱码 所以默认显示为CC？<br/>fwrite函数在写文件时是以二进制形式进行的。<br/>函数原型：int fwrite(char *ptr, unsigned size, unsigned n, FILE *fp);<br/><br/>功&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 能：将ptr所指向的n*size个字节输出到fp所指向的文件中<br/><br/>返 回&nbsp;&nbsp;值：写到fp文件中的数据项的个数<br/><textarea name="code" class="php" rows="15" cols="100">
// 定义一个学生结构体
struct Student_type
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;char name[10];
&nbsp;&nbsp;&nbsp;&nbsp;int num;
&nbsp;&nbsp;&nbsp;&nbsp;int age;
&nbsp;&nbsp;&nbsp;&nbsp;char addr[30];
&#125;stud[40];
int i;
FILE *fp;&nbsp;&nbsp;// 定义一个文件指针fp
fp = fopen(&quot;stu.dat&quot;, &quot;wb&quot;);&nbsp;&nbsp;// 以二进制可写方式打开stu.dat文件
// 将40个学生的记录写入文件stu.dat中
for(i=0; i&lt;40; i++)
&nbsp;&nbsp;&nbsp;&nbsp;fwrite(&amp;stud[i], sizeof(struct Student_type), 1, fp);
</textarea><br/>实践：<br/><textarea name="code" class="php" rows="15" cols="100">
if (recvlen == msglen)//循环终止条件之一：当前待处理TCP消息恰好为一条完整的应用层消息
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&#92;n&#92;n运行的包结束了，指向这儿：jackxiang Here...XXXXXXXX000000000XXXXXXX,recvlen=%d,buf=%s&#92;n&#92;n&quot;,recvlen,buf);
&nbsp;&nbsp;&nbsp;&nbsp;FILE *dst;
&nbsp;&nbsp;&nbsp;&nbsp;dst=fopen(&quot;/tmp/out.dat&quot;,&quot;a+&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;if(dst==NULL)&#123;
&nbsp;&nbsp;printf(&quot;cant&#039;to open destination file&#92;n&quot;);
&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;write to /tmp/out.dat&#039;s recvlen&#039;s length=%d&#92;n&quot;,strlen(buf));
&nbsp;&nbsp;&nbsp;&nbsp;fwrite(buf,recvlen,1,dst);
&nbsp;&nbsp;&nbsp;&nbsp;fclose(dst);
&nbsp;&nbsp;&nbsp;&nbsp;rs=0;
&nbsp;&nbsp;&nbsp;&nbsp;buf = NULL;
&nbsp;&nbsp;&nbsp;&nbsp;break;
&#125;
</textarea>
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]为何数组的首地址当指针传入函数时，得传入数组的长度之sizeof函数，sizeof(数组名)和sizeof(指针)的区别，及fwrite里的第二第三参数的用法。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>