<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>What will happen if I pass an element of a &amp;#39;char&amp;#39; array to a function expecting an int</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/39642/what-will-happen-if-i-pass-an-element-of-a-char-array-to-a-function-expecting-an-int</link><description> 
I know this is probably a basic question about storing of
variables in memory but... 

 
First, if I take a 16bit integer, split it into 2 bytes, then
place these into adjacent elements in a char array. 

 
Then, if I call a function expecting to be</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: What will happen if I pass an element of a 'char' array to a function expecting an int</title><link>https://community.arm.com/thread/60482?ContentTypeID=1</link><pubDate>Wed, 07 Mar 2012 11:38:01 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:3d32f184-834e-490b-be62-6e7bbfd7ab47</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;Then, if I call a function expecting to be passed a 16bit
integer and give it an element of the char array.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;Will it copy the contents of the array element specified and
then copy the next one automatically too?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Of course not. C does what&amp;#39;s called &amp;quot;pass by value&amp;quot;, i.e. the
called function gets the &lt;b&gt;value&lt;/b&gt; of what you passed it. In the
case at hand, that&amp;#39;s a number between 0 and 255 (or -128 and 127,
depending on a compiler switch).&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;Is this bad programming?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Not yet. But the way you&amp;#39;re asking the question indicates you&amp;#39;re
setting off in the wrong direction.&lt;/p&gt;

&lt;p&gt;
In the case you present, there&amp;#39;s no need whatsoever to rip your
16-bit unsigned into individual bytes, thus no need to worry what
will happen to it once it&amp;#39;s split up. You have a uint16_t object, and
a function that wants exactly such a thing for its argument. Why
convert/split/join anything?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>