<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Effective C Tip #7 &#8211; Use strongly typed function parameters</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Fri, 10 Feb 2012 14:37:44 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Anders</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/comment-page-1/#comment-4027</link>
		<dc:creator>Anders</dc:creator>
		<pubDate>Fri, 06 May 2011 07:03:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/12/effective-c-tip-7-use-strongly-typed-function-parameters/#comment-4027</guid>
		<description>&quot;ISO 8601 Data elements and interchange formats — Information interchange — Representation of dates and times&quot;
:-)</description>
		<content:encoded><![CDATA[<p>&#8220;ISO 8601 Data elements and interchange formats — Information interchange — Representation of dates and times&#8221;<br />
 <img src='http://embeddedgurus.com/stack-overflow/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/comment-page-1/#comment-3947</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Sat, 30 Apr 2011 00:01:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/12/effective-c-tip-7-use-strongly-typed-function-parameters/#comment-3947</guid>
		<description>That&#039;s very interesting Jorg. I don&#039;t recollect having seen this before.</description>
		<content:encoded><![CDATA[<p>That&#8217;s very interesting Jorg. I don&#8217;t recollect having seen this before.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jörg Seebohn</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/comment-page-1/#comment-3942</link>
		<dc:creator>Jörg Seebohn</dc:creator>
		<pubDate>Fri, 29 Apr 2011 15:26:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/12/effective-c-tip-7-use-strongly-typed-function-parameters/#comment-3942</guid>
		<description>The typedef SERIAL_STR is not standard:

You should declare it like:

   typedef char SERIAL_STR[16] ;

Every array parameter is treated as a pointer parameter. 

But there is a trick if you want to let the compiler determine the correct array size:

void test_func(SERIAL_STR * param)
{
   printf(&quot;sizeof(*param)=%u&quot;, sizeof(*param)) ;
}

// prints sizeof(*param)=16</description>
		<content:encoded><![CDATA[<p>The typedef SERIAL_STR is not standard:</p>
<p>You should declare it like:</p>
<p>   typedef char SERIAL_STR[16] ;</p>
<p>Every array parameter is treated as a pointer parameter. </p>
<p>But there is a trick if you want to let the compiler determine the correct array size:</p>
<p>void test_func(SERIAL_STR * param)<br />
{<br />
   printf(&#8220;sizeof(*param)=%u&#8221;, sizeof(*param)) ;<br />
}</p>
<p>// prints sizeof(*param)=16</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/comment-page-1/#comment-3940</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Fri, 29 Apr 2011 14:32:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/12/effective-c-tip-7-use-strongly-typed-function-parameters/#comment-3940</guid>
		<description>Hmmm, that may well explain it. I normally find GCC to be very good on issues such as this, so perhaps it&#039;s something Microchip has done as a &#039;special extension&#039;?</description>
		<content:encoded><![CDATA[<p>Hmmm, that may well explain it. I normally find GCC to be very good on issues such as this, so perhaps it&#8217;s something Microchip has done as a &#8216;special extension&#8217;?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dirk</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/comment-page-1/#comment-3939</link>
		<dc:creator>Dirk</dc:creator>
		<pubDate>Fri, 29 Apr 2011 13:48:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/12/effective-c-tip-7-use-strongly-typed-function-parameters/#comment-3939</guid>
		<description>I&#039;m using the Microchip C compiler for dsPIC30 (based on gcc version 4.0.3).</description>
		<content:encoded><![CDATA[<p>I&#8217;m using the Microchip C compiler for dsPIC30 (based on gcc version 4.0.3).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/comment-page-1/#comment-3937</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Fri, 29 Apr 2011 12:42:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/12/effective-c-tip-7-use-strongly-typed-function-parameters/#comment-3937</guid>
		<description>The same thought had crossed my mind as well. I just dropped the code into my IAR ARM compiler. It gives the following errors on your typedef:
Error[Pe040]: expected an identifier test.c 114 
Error[Pe065]: expected a &quot;;&quot; test.c 114 
What compiler are you using?</description>
		<content:encoded><![CDATA[<p>The same thought had crossed my mind as well. I just dropped the code into my IAR ARM compiler. It gives the following errors on your typedef:<br />
Error[Pe040]: expected an identifier test.c 114<br />
Error[Pe065]: expected a &#8220;;&#8221; test.c 114<br />
What compiler are you using?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dirk</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/comment-page-1/#comment-3935</link>
		<dc:creator>Dirk</dc:creator>
		<pubDate>Fri, 29 Apr 2011 11:53:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/12/effective-c-tip-7-use-strongly-typed-function-parameters/#comment-3935</guid>
		<description>But wouldn&#039;t it be more reasonable to give a compile error? It seems wrong to me that they are different although they are &quot;declared&quot; in the same way.</description>
		<content:encoded><![CDATA[<p>But wouldn&#8217;t it be more reasonable to give a compile error? It seems wrong to me that they are different although they are &#8220;declared&#8221; in the same way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/comment-page-1/#comment-3933</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Fri, 29 Apr 2011 10:14:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/12/effective-c-tip-7-use-strongly-typed-function-parameters/#comment-3933</guid>
		<description>I&#039;m glad you find the articles useful. While a typedef allows you to create a shorthand for a data type, it doesn&#039;t allow you to break the language rules. In C you can&#039;t pass an array to a function by value - only by reference. Thus given that SERIAL_STR is typedef&#039;d to be char[16], the compiler is simply converting your attempt to pass by value into a pass by reference - i.e. a pointer</description>
		<content:encoded><![CDATA[<p>I&#8217;m glad you find the articles useful. While a typedef allows you to create a shorthand for a data type, it doesn&#8217;t allow you to break the language rules. In C you can&#8217;t pass an array to a function by value &#8211; only by reference. Thus given that SERIAL_STR is typedef&#8217;d to be char[16], the compiler is simply converting your attempt to pass by value into a pass by reference &#8211; i.e. a pointer</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dirk</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/comment-page-1/#comment-3932</link>
		<dc:creator>Dirk</dc:creator>
		<pubDate>Fri, 29 Apr 2011 09:55:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/12/effective-c-tip-7-use-strongly-typed-function-parameters/#comment-3932</guid>
		<description>Hi Nigel!
I learned a lot from your articles, thank you very much!
So after reading this article about strongly typed parameters, I stumbled upon a problem with typedefs as function parameters. See this code:

typedef char[16] SERIAL_STR;

void test_func(SERIAL_STR param)
{
	SERIAL_STR local_var;
	printf(&quot;sizeof(param)=%u\n&quot;,sizeof(param));
	printf(&quot;sizeof(SERIAL_STR)=%u\n&quot;,sizeof(SERIAL_STR));
	printf(&quot;sizeof(local_var)=%u\n&quot;,sizeof(local_var));
}

On a 16bit dsPIC the output is:
sizeof(param)=2
sizeof(SERIAL_STR)=16
sizeof(local_var)=16

When the typedef is a function parameter, it seems to be treated like a pointer by the compiler.</description>
		<content:encoded><![CDATA[<p>Hi Nigel!<br />
I learned a lot from your articles, thank you very much!<br />
So after reading this article about strongly typed parameters, I stumbled upon a problem with typedefs as function parameters. See this code:</p>
<p>typedef char[16] SERIAL_STR;</p>
<p>void test_func(SERIAL_STR param)<br />
{<br />
	SERIAL_STR local_var;<br />
	printf(&#8220;sizeof(param)=%u\n&#8221;,sizeof(param));<br />
	printf(&#8220;sizeof(SERIAL_STR)=%u\n&#8221;,sizeof(SERIAL_STR));<br />
	printf(&#8220;sizeof(local_var)=%u\n&#8221;,sizeof(local_var));<br />
}</p>
<p>On a 16bit dsPIC the output is:<br />
sizeof(param)=2<br />
sizeof(SERIAL_STR)=16<br />
sizeof(local_var)=16</p>
<p>When the typedef is a function parameter, it seems to be treated like a pointer by the compiler.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/10/effective-c-tip-7-use-strongly-typed-function-parameters/comment-page-1/#comment-3257</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Sat, 29 Jan 2011 20:37:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/10/12/effective-c-tip-7-use-strongly-typed-function-parameters/#comment-3257</guid>
		<description>I tend to think of enumerated values as &#039;typed constants&#039;. Like you because they are constant I think they should be capitalized. The type side of enumerations I don&#039;t usually worry about too much, in part because I have Lint set up to complain loudly about questionable type conversions involving enumerated types.</description>
		<content:encoded><![CDATA[<p>I tend to think of enumerated values as &#8216;typed constants&#8217;. Like you because they are constant I think they should be capitalized. The type side of enumerations I don&#8217;t usually worry about too much, in part because I have Lint set up to complain loudly about questionable type conversions involving enumerated types.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

