<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: handling variable length arrays? in CodeWarrior for MCU</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/handling-variable-length-arrays/m-p/220225#M8772</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello irob,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Strictly speaking, when you pass an array name to a function, this represents the &lt;U&gt;address&lt;/U&gt; of the first element of the array, i.e. a pointer to the array.&amp;nbsp; In this reapect, array variables differ from other&amp;nbsp;variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The string manipulation library functions would also use a similar method.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 02 Sep 2010 11:08:26 GMT</pubDate>
    <dc:creator>bigmac</dc:creator>
    <dc:date>2010-09-02T11:08:26Z</dc:date>
    <item>
      <title>handling variable length arrays?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/handling-variable-length-arrays/m-p/220222#M8769</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is there a good way of handling variable length arrays that I'm just not seeing?&amp;nbsp; Here's what I have.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm writing a general purpose I2C library with all the usual suspects, like Start, Stop, Ack, Nack, Write, and Read functions, etc.&amp;nbsp; For the Read function, I'd like to accommodate a range of I2C slave peripherals.&amp;nbsp; For instance, I've used devices that have no registers, some that do have registers, the majority of which only return single octets of data at a time when read, but some that return multiple octets.&amp;nbsp; So I figured it would be cool to make the Read function such that I could send it how many bytes to read back.&amp;nbsp; Well, this requires some allocated memory block, say an array, to dump into.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But if this Read function is general purpose, then the array depth is not known at compile time.&amp;nbsp; I tried writing my function to return a pointer to a static memory location.&amp;nbsp; Trouble is, the compiler only allocates the first data byte pointed to by this pointer.&amp;nbsp; The rest of the data is in unallocated space and might get trampled on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I finally gave up and just created a global array that I knew would be big enough for most devices.&amp;nbsp; Then I call my Read function and pass it a pointer to this array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Not elegant!&amp;nbsp; Any suggestions?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Sep 2010 02:47:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/handling-variable-length-arrays/m-p/220222#M8769</guid>
      <dc:creator>irob</dc:creator>
      <dc:date>2010-09-01T02:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: handling variable length arrays?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/handling-variable-length-arrays/m-p/220223#M8770</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello irob,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I might suggest that the general purpose IIC function library should not contain any code that directly relates to a specific IIC slave device.&amp;nbsp; The buffer&amp;nbsp;array variable, and its size, required&amp;nbsp;by the read process should therefore be defined outside of the function library.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The read function within the library would then be passed a pointer to the&amp;nbsp;start of the&amp;nbsp;buffer, plus the number of bytes to be read.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe&amp;nbsp;the following prototype for the library function:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;void IIC_read( byte slave_addr,&amp;nbsp;byte *buf, byte n);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;#define IIC_BUFSIZE&amp;nbsp; 4&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;#define SLAVE_ADDR&amp;nbsp; &amp;nbsp;0x3C&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;byte IIC_rdbuf[IIC_BUFSIZE]&amp;nbsp; // Define buffer array&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;...&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;IIC_read( SLAVE_ADDR, IIC_rdbuf, IIC_BUFSIZE);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Multiple buffers could be defined if more than one IIC device, without affecting the library functions.&amp;nbsp; The buffer would not necessarily need to be a global variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Sep 2010 08:44:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/handling-variable-length-arrays/m-p/220223#M8770</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2010-09-01T08:44:09Z</dc:date>
    </item>
    <item>
      <title>Re: handling variable length arrays?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/handling-variable-length-arrays/m-p/220224#M8771</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You and I were thinking along the same lines, bigmac.&amp;nbsp; But you went one further by suggesting essentially separate read arrays for each IIC slave device.&amp;nbsp; I like this idea and have adopted it.&amp;nbsp; Until your example, I hadn't realized you could pass an array to a function.&amp;nbsp; Thought only pointers were legal.&amp;nbsp; Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Sep 2010 20:41:28 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/handling-variable-length-arrays/m-p/220224#M8771</guid>
      <dc:creator>irob</dc:creator>
      <dc:date>2010-09-01T20:41:28Z</dc:date>
    </item>
    <item>
      <title>Re: handling variable length arrays?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/handling-variable-length-arrays/m-p/220225#M8772</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello irob,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Strictly speaking, when you pass an array name to a function, this represents the &lt;U&gt;address&lt;/U&gt; of the first element of the array, i.e. a pointer to the array.&amp;nbsp; In this reapect, array variables differ from other&amp;nbsp;variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The string manipulation library functions would also use a similar method.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Sep 2010 11:08:26 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/handling-variable-length-arrays/m-p/220225#M8772</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2010-09-02T11:08:26Z</dc:date>
    </item>
  </channel>
</rss>

