﻿<?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++博客-RNAMatrix</title><link>http://www.cppblog.com/matrix/</link><description>Coffee in, Code out</description><language>zh-cn</language><lastBuildDate>Tue, 14 Apr 2026 23:06:09 GMT</lastBuildDate><pubDate>Tue, 14 Apr 2026 23:06:09 GMT</pubDate><ttl>60</ttl><item><title>C++ 标准库函数与宏定义的名字冲突 </title><link>http://www.cppblog.com/matrix/archive/2010/08/05/122329.html</link><dc:creator>matrix</dc:creator><author>matrix</author><pubDate>Thu, 05 Aug 2010 09:12:00 GMT</pubDate><guid>http://www.cppblog.com/matrix/archive/2010/08/05/122329.html</guid><wfw:comment>http://www.cppblog.com/matrix/comments/122329.html</wfw:comment><comments>http://www.cppblog.com/matrix/archive/2010/08/05/122329.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/matrix/comments/commentRss/122329.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/matrix/services/trackbacks/122329.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 今天在使用limits中的numeric_limits类模板函数max和min时，编译出现了一个错误：view plaincopy to clipboardprint?error&nbsp;C2589:&nbsp;'('&nbsp;:&nbsp;illegal&nbsp;token&nbsp;on&nbsp;right&nbsp;side&nbsp;of&nbsp;'::'&nbsp;&nbsp;...&nbsp;&nbsp;<a href='http://www.cppblog.com/matrix/archive/2010/08/05/122329.html'>阅读全文</a><img src ="http://www.cppblog.com/matrix/aggbug/122329.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/matrix/" target="_blank">matrix</a> 2010-08-05 17:12 <a href="http://www.cppblog.com/matrix/archive/2010/08/05/122329.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> C++ 函数的数组参数</title><link>http://www.cppblog.com/matrix/archive/2010/07/08/119754.html</link><dc:creator>matrix</dc:creator><author>matrix</author><pubDate>Thu, 08 Jul 2010 09:30:00 GMT</pubDate><guid>http://www.cppblog.com/matrix/archive/2010/07/08/119754.html</guid><wfw:comment>http://www.cppblog.com/matrix/comments/119754.html</wfw:comment><comments>http://www.cppblog.com/matrix/archive/2010/07/08/119754.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/matrix/comments/commentRss/119754.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/matrix/services/trackbacks/119754.html</trackback:ping><description><![CDATA[
<p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">我们都知道C和C++中在传递一维数组时,，都将被编译器视为指针类型。如：</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><div class="dp-highlighter" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: rgb(231, 229, 220); width: 1008px; overflow-x: auto; overflow-y: auto; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; line-height: 21px; "><div class="bar" style="padding-left: 45px; "><div class="tools" style="padding-top: 3px; padding-right: 8px; padding-bottom: 10px; padding-left: 10px; font: normal normal normal 9px/normal Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); "><a href="http://blog.csdn.net/RNAMatrix/archive/2010/07/08/5720274.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">view plain</a><a href="http://blog.csdn.net/RNAMatrix/archive/2010/07/08/5720274.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">copy to clipboard</a><a href="http://blog.csdn.net/RNAMatrix/archive/2010/07/08/5720274.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">print</a><a href="http://blog.csdn.net/RNAMatrix/archive/2010/07/08/5720274.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">?</a></div></div><ol start="1" class="dp-c" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: decimal; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); "><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-position: outside !important; list-style-image: initial; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="comment" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 130, 0); background-color: inherit; ">//&nbsp;三个等价的putValues()声明&nbsp;</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;</span></span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-position: outside !important; list-style-image: initial; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">void</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;func(&nbsp;</span><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">int</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">*&nbsp;);&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-position: outside !important; list-style-image: initial; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">void</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;func(&nbsp;</span><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">int</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">[]&nbsp;);&nbsp;&nbsp;</span></span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-position: outside !important; list-style-image: initial; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">void</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;func(&nbsp;</span><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">int</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">[&nbsp;10&nbsp;]&nbsp;);&nbsp;&nbsp;&nbsp;</span></span></li></ol></div><p style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">所以传递一个指针实参给一维数组参数是可行的。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">而且，当编译器对实参类型进行参数类型检查时并不检查一维数组的长度，也就是说数组的长度与参数声明无关。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">那么上升为多维数组的情况，多维数组参数也将会被编译器视为指针的指针（或者说指针数组）吗？</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">当然不是，当函数参数是多维数组时，这样的参数必须指明第一维以外的所有维的长度。例如：</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><div class="dp-highlighter" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: rgb(231, 229, 220); width: 1008px; overflow-x: auto; overflow-y: auto; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; line-height: 21px; "><div class="bar" style="padding-left: 45px; "><div class="tools" style="padding-top: 3px; padding-right: 8px; padding-bottom: 10px; padding-left: 10px; font: normal normal normal 9px/normal Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); "><a href="http://blog.csdn.net/RNAMatrix/archive/2010/07/08/5720274.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">view plain</a><a href="http://blog.csdn.net/RNAMatrix/archive/2010/07/08/5720274.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">copy to clipboard</a><a href="http://blog.csdn.net/RNAMatrix/archive/2010/07/08/5720274.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">print</a><a href="http://blog.csdn.net/RNAMatrix/archive/2010/07/08/5720274.aspx#" style="text-decoration: none; color: rgb(160, 160, 160); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">?</a></div></div><ol start="1" class="dp-c" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: decimal; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); "><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-position: outside !important; list-style-image: initial; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">void</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;func(&nbsp;</span><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">int</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;matrix[][10],&nbsp;...);&nbsp;&nbsp;&nbsp;</span></span></li><li class="" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-position: outside !important; list-style-image: initial; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(248, 248, 248); color: rgb(92, 92, 92); line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="comment" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 130, 0); background-color: inherit; ">//等价声明</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;</span></span></li><li class="alt" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; list-style-type: decimal-leading-zero; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-width: initial; border-color: initial; list-style-position: outside !important; list-style-image: initial; border-left-width: 3px; border-left-color: rgb(108, 226, 108); background-color: rgb(255, 255, 255); color: inherit; line-height: 14px; "><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">void</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;func(&nbsp;</span><span class="keyword" style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">int</span><span style="line-height: 18px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;(*matrix)[10]&nbsp;);&nbsp;&nbsp;&nbsp;</span></span></li></ol></div><p style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">我们可以看到，二维数组的将会被编译器视为数组指针，而非指针的指针或者指针数组。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">这也就是为什么我们经常需要传递一个二维数组参数时误传了一个指针的指针或使得编译器报错的原因。</p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; ">最后来总结一下：</p><div style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><span style="color: rgb(0, 128, 0); ">****************************************************************************************************&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div><div style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><span style="color: rgb(0, 128, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 实参&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 所匹配的形式参数</span></div><div style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><span style="color: rgb(0, 128, 0); ">数组的数组&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char c[m][n]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char(*)[n]&nbsp;&nbsp;&nbsp; -&gt; 数组指针</span></div><div style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><span style="color: rgb(0, 128, 0); ">指针数组&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *c[n]&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char **c&nbsp;&nbsp;&nbsp; -&gt; 指针的指针</span></div><div style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><span style="color: rgb(0, 128, 0); ">数组指针&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char (*c)[n]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char (*c)[n] &nbsp;&nbsp; -&gt; 不改变</span></div><div style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><span style="color: rgb(0, 128, 0); ">指针的指针&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char **c;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char **c &nbsp; -&gt;&nbsp; 不改变</span></div><div style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><span style="color: rgb(0, 128, 0); "><br></span></div><div style="font-family: verdana, sans-serif; font-size: 14px; line-height: 21px; "><span style="color: rgb(0, 128, 0); ">***************************************************************************************************</span>&nbsp;<span style="color: rgb(0, 128, 0); ">*/</span></div><img src ="http://www.cppblog.com/matrix/aggbug/119754.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/matrix/" target="_blank">matrix</a> 2010-07-08 17:30 <a href="http://www.cppblog.com/matrix/archive/2010/07/08/119754.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>浅谈C中malloc和memset函数</title><link>http://www.cppblog.com/matrix/archive/2010/07/07/119531.html</link><dc:creator>matrix</dc:creator><author>matrix</author><pubDate>Tue, 06 Jul 2010 23:44:00 GMT</pubDate><guid>http://www.cppblog.com/matrix/archive/2010/07/07/119531.html</guid><wfw:comment>http://www.cppblog.com/matrix/comments/119531.html</wfw:comment><comments>http://www.cppblog.com/matrix/archive/2010/07/07/119531.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/matrix/comments/commentRss/119531.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/matrix/services/trackbacks/119531.html</trackback:ping><description><![CDATA[


在C中 malloc和memset是2个常用的对内存操作的函数。首先还是来看一下这2个函数的函数原型。<div>1.malloc函数</div><div>malloc函数用于从堆上分配指定字节的内存空间。</div><div><br></div><div><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;malloc(</span><font color="#0000FF">size_t&nbsp;</font><span style="color: #000000; ">n);</span></div><br></div><div><br></div><div>其中，形参n为要求分配的内存字节数。如果执行成功，函数范围获得的内存空间的首地址；执行失败，返回值为NULL。由于函数返回值值的类型为void的指针，因此，可以将void指针类型转换后赋值给任意类型指针，这样就可以通过操作该类型指针来操作从堆上获得的内存空间。</div><div>需要注意的是malloc函数分配得到的内存空间是未初始化的。有时候，在使用前需要对该内存空间进行初始化，memset就派上用场了。</div><div><br></div><div>2.memset函数</div><div><br><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;memset&nbsp;(</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;p,</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;c,</span><span style="color: #0000FF; ">size_t</span><span style="color: #000000; ">&nbsp;n);</span></div><br></div><div>其中，指针p为所操作的内存空间的首地址，c为每个字节所赋的值，n为所操作内存空间的字节长度，也就是内存被赋值为c的字节数。</div><div>在使用memset时经常要注意的它是以字节为单位进行赋值的，所赋值的范围是0x00～0xFF。若想要对一个double或int型的数组赋值时，就特别需要注意这一点：</div><div><br></div><div><div style="padding-right: 5px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(238, 238, 238); padding-bottom: 4px; border-left-color: rgb(204, 204, 204); padding-top: 4px; font-size: 13px; width: 443px; word-break: break-all; color: rgb(0, 0, 255); font-family: monospace; white-space: pre; "><span style="color: rgb(0, 0, 255); ">example1:</span></div><div style="padding-right: 5px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(238, 238, 238); padding-bottom: 4px; border-left-color: rgb(204, 204, 204); padding-top: 4px; font-size: 13px; width: 443px; word-break: break-all; color: rgb(0, 0, 255); font-family: monospace; white-space: pre; "><span style="color: rgb(0, 0, 255); ">char </span><span style="color: rgb(0, 0, 0); ">a[4</span><span style="color: rgb(0, 0, 0); ">];</span></div><div style="padding-right: 5px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(238, 238, 238); padding-bottom: 4px; border-left-color: rgb(204, 204, 204); padding-top: 4px; font-size: 13px; width: 443px; word-break: break-all; color: rgb(0, 0, 255); font-family: monospace; white-space: pre; "><span style="color: rgb(0, 0, 0); ">memset(a, '\0', 4);</span></div><div style="padding-right: 5px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(238, 238, 238); padding-bottom: 4px; border-left-color: rgb(204, 204, 204); padding-top: 4px; font-size: 13px; width: 443px; word-break: break-all; color: rgb(0, 0, 255); font-family: monospace; white-space: pre; "><span style="color: rgb(0, 0, 0); "><br></span></div><div style="padding-right: 5px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(238, 238, 238); padding-bottom: 4px; border-left-color: rgb(204, 204, 204); padding-top: 4px; font-size: 13px; width: 443px; word-break: break-all; color: rgb(0, 0, 255); font-family: monospace; white-space: pre; "><span style="color: rgb(0, 0, 255); ">example2:</span></div><div style="padding-right: 5px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(238, 238, 238); padding-bottom: 4px; border-left-color: rgb(204, 204, 204); padding-top: 4px; font-size: 13px; width: 443px; word-break: break-all; color: rgb(0, 0, 255); font-family: monospace; white-space: pre; "><span style="color: rgb(0, 0, 255); "></span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&nbsp;a[4</span><span style="color: rgb(0, 0, 0); ">];</span></div><div style="padding-right: 5px; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(238, 238, 238); padding-bottom: 4px; border-left-color: rgb(204, 204, 204); padding-top: 4px; font-size: 13px; width: 443px; word-break: break-all; color: rgb(0, 0, 255); font-family: monospace; white-space: pre; "><span style="color: rgb(0, 0, 0); ">memset(a, </span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">, 4</span><span style="color: rgb(0, 0, 0); ">);</span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">这里改成memset(a,1,5*sizeof(int))也是不可以的</span></div></div><div><br></div><div>第一个例子是正确的，memset对字符数组里的每个char类型元素赋值为NULL。第二个例子显然得到的结果不是把int型数组里的每个元素赋值为了1。因为memset函数以字节为单位进行赋值，那么数组中一个int型元素的4个字节都将被赋值为1(或者说ASCII码1所对应的字符)，实际上它所表示的整数是0x01010101。</div><div>另外，在使用malloc为一个二维数组分配内存空间时，要特别注意使用memset进行初始化可能会出错。</div><div><br></div><div><div style="background-color: rgb(238, 238, 238); font-size: 13px; border-left-color: rgb(204, 204, 204); padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;m&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">2</span><span style="color: #000000; ">;<br></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;n&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">3</span><span style="color: #000000; ">;<br></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i;<br><br></span><span style="color: #008000; ">//</span><span style="color: #008000; ">二位数组a[m][n]</span><span style="color: #008000; "><br></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">**</span><span style="color: #000000; ">a;<br><br>a&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">**</span><span style="color: #000000; ">)&nbsp;malloc(m&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">));<br><br></span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(i</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;&nbsp;i</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">m;&nbsp;</span><span style="color: #000000; ">++</span><span style="color: #000000; ">i)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;a[i]&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)&nbsp;malloc(n&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">));<br>}<br><br>memset(a,&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,&nbsp;</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">)&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;m&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;n);</span></div></div><div><br></div><div>对所有二维以上的数组使用memset时，若此多维数组是通过多次调用malloc函数搭积木分配得到的，那么该数组的内存空间可能不连续。使用memset函数进行连续的统一赋值就毫无意义了。直接声明的多维数组如a[2][3]的内存空间显然是连续的，此时使用memset函数初始化就没有问题。</div><img src ="http://www.cppblog.com/matrix/aggbug/119531.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/matrix/" target="_blank">matrix</a> 2010-07-07 07:44 <a href="http://www.cppblog.com/matrix/archive/2010/07/07/119531.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>