<?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>Classic/Legacy CodeWarriorのトピックRe: passing fixed data and variables to function using pointers</title>
    <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141985#M1428</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;I was wanting to use the function to output from a const list OR from a RAM based variable. Won't the compiler throw a wobbly if I try to pass a reference to a variable (unsigned char) rather than a const if I add the const to the argument list.&lt;BR /&gt;&lt;BR /&gt;I shall not be outputting binary so I note your comments on terminating conditions. I will have another similar routine though that will output binary so will have to include a length byte somewhere.&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 03 Aug 2007 01:47:58 GMT</pubDate>
    <dc:creator>stevec</dc:creator>
    <dc:date>2007-08-03T01:47:58Z</dc:date>
    <item>
      <title>passing fixed data and variables to function using pointers</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141982#M1425</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a number of predefined data strings and also a variable area which I wish to be accessed by a function which sends&amp;nbsp; the data bytes to an output routine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have assigned the fixed strings as&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; const unsigned char string1[] = "This is string 1";&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; const unsigned char string2[] = "And this is another string";&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; etc&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the variable is in&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; unsigned char temp_store[256];&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am passing reference using&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; size = sizeof(store1) -1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; send_data_to_output(store1, size);&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OR&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; size = sizeof(temp_store) - 1;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; //assume a NUL added to end of data as terminator&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; send_data_to_output(temp_store, size)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;the sending routine is defines as&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void send_data_to_output(unsigned char *pointer, unsigned char data_size)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;When ' temp_store' is passed it compiles ok but when I try to pass 'string1' for example it gives me an "Indirection to different types" error. I can see why its doing it as one is const and the other isnt. How do I get my routine to work with both constant and variable data? without having first to transfer the const data into a variable?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Steve&lt;/SPAN&gt;&lt;BR /&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2007 17:51:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141982#M1425</guid>
      <dc:creator>stevec</dc:creator>
      <dc:date>2007-08-02T17:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: passing fixed data and variables to function using pointers</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141983#M1426</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello Steve,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;When I compiled similar code, the compiler issued a &lt;U&gt;warning&lt;/U&gt; (C1285), rather than an error,&amp;nbsp;so the compile and link process actually&amp;nbsp;successfully completed.&amp;nbsp; When run in the simulator, the code seemed to give the required result, in spite of the warning.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;A further comment - for string data, why do you need the 'data_size' variable?&amp;nbsp; A potentially simpler alterative would be to output each character until a null byte&amp;nbsp;is reached.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;Message Edited by bigmac on &lt;SPAN class="date_text"&gt;2007-08-03&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;02:03 AM&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2007 23:02:03 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141983#M1426</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2007-08-02T23:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: passing fixed data and variables to function using pointers</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141984#M1427</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;The ColdFire compiler and other MW compilers are more strict than the 8/16 bit compilers when implicitely loosing the constness. Regardless, I would always code const correct in the first place so the 8/16 bit compiler do not issue a warning and the other MW compiler have no reason to issue an error.&lt;BR /&gt;&lt;BR /&gt;With the assumption that send_data_to_output is not modifying the data to be sent, just add the const to the&lt;BR /&gt;argument list:&lt;BR /&gt;&lt;BR /&gt;extern void send_data_to_output(const unsigned char *pointer, unsigned char data_size);&lt;BR /&gt;&lt;BR /&gt;It's not a problem for the compiler to add a const implicitely (in the first indirection), just to loose the const is the bad thing.&lt;BR /&gt;&lt;BR /&gt;If the data_size parameter is necessary probably depends on if this function is only used to send out zero terminated strings, or if it is used to send binary data too.&lt;BR /&gt;&lt;BR /&gt;Daniel&lt;BR /&gt;&lt;BR /&gt;PS: Please always mention the architecture, compiler version, copy paste the error message,...&lt;BR /&gt;&lt;BR /&gt;PS: adding a const to the argument usually propagates further in the code. But most of the time it should not be necessary to add a cast to shutup the compiler. Just when you get to some 3'rd party sources you don't want to adapt.&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;Yet another PS: probably it should be "size = strlen(temp_store)" and not sizeof()-1 :smileywink:&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Aug 2007 00:46:54 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141984#M1427</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2007-08-03T00:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: passing fixed data and variables to function using pointers</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141985#M1428</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;I was wanting to use the function to output from a const list OR from a RAM based variable. Won't the compiler throw a wobbly if I try to pass a reference to a variable (unsigned char) rather than a const if I add the const to the argument list.&lt;BR /&gt;&lt;BR /&gt;I shall not be outputting binary so I note your comments on terminating conditions. I will have another similar routine though that will output binary so will have to include a length byte somewhere.&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Aug 2007 01:47:58 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141985#M1428</guid>
      <dc:creator>stevec</dc:creator>
      <dc:date>2007-08-03T01:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: passing fixed data and variables to function using pointers</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141986#M1429</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello Steve,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Another approach, that seems to work at least&amp;nbsp;on 8-bit MCUs, without error or warning, is to use a cast when referencing&amp;nbsp;constant string data.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;send_data_to_output((unsigned char *)string1, ... );&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;And for a RAM based array,&amp;nbsp;a cast would not be required.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;To handle both null terminated string data, and binary data, in the one function might be done more easily by simply using a length value of zero for string data, and specifying the actual length for binary data.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;void send_data_to_output (unsigned char&amp;nbsp;*pointer,&amp;nbsp;unsigned char&amp;nbsp;data_size)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; byte i;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if (data_size) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (i = 0; i &amp;lt; data_size; i++)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; send_byte (*pointer++);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp;&amp;nbsp; else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while (*pointer)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; send_byte (*pointer++);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;}&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Aug 2007 03:16:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141986#M1429</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2007-08-03T03:16:16Z</dc:date>
    </item>
    <item>
      <title>Re: passing fixed data and variables to function using pointers</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141987#M1430</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;FONT face="Courier New" size="2"&gt;Just add the const to the declaration of&lt;/FONT&gt; &lt;FONT face="Courier New" size="2"&gt;send_data_to_output,&lt;BR /&gt;and both the const and non const arrays can be passed with no cast (or warning)&lt;BR /&gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;gt;void send_data_to_output(const unsigned char&amp;nbsp;*pointer,&lt;BR /&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; unsigned char&amp;nbsp;data_size)&lt;/FONT&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;BR /&gt;&lt;BR /&gt;Daniel&lt;BR /&gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;BTW, while it will just work fine, I personally do not like magicargument values like that "data_size == 0" does mean "print the other argument as a zero terminated string".&lt;BR /&gt;For me sending a zero terminated string and sending n-char's are separate things to do and should have their own separate function.&lt;BR /&gt;For unification, the print zero terminated string can easely be mapped with a strlen to the print n-char's.&lt;BR /&gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Aug 2007 05:50:53 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/passing-fixed-data-and-variables-to-function-using-pointers/m-p/141987#M1430</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2007-08-03T05:50:53Z</dc:date>
    </item>
  </channel>
</rss>

