<?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>CodeWarrior for MCUのトピックRe: CW4.5 S12X Initializing unions</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150216#M3553</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;I still don't see any problem here...&lt;BR /&gt;&lt;BR /&gt;typedef union&lt;BR /&gt;{&lt;BR /&gt;struct&lt;BR /&gt;{&lt;BR /&gt;int someInt;&lt;BR /&gt;float someFloat;&lt;BR /&gt;&lt;BR /&gt;/* add more members, nested structs, arrays... */&lt;BR /&gt;&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;SomeOtherStructure a;&lt;BR /&gt;SomeVeryComplicatedStructure b;&lt;BR /&gt;unsigned char bytes[x];&lt;BR /&gt;/* add as many as you like... /*&lt;BR /&gt;&lt;BR /&gt;} SomeUnion;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;const SomeUnion myUnion = {123, 3.14, /*...*/ };&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 22 Oct 2007 20:31:32 GMT</pubDate>
    <dc:creator>Lundin</dc:creator>
    <dc:date>2007-10-22T20:31:32Z</dc:date>
    <item>
      <title>CW4.5 S12X Initializing unions</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150210#M3547</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;P&gt;Is there a way in CW to control type when initializing union members?&lt;/P&gt;&lt;P&gt;In the code below, all the param elements are initialized as integers even though some are ints and some are floats. If I change the declaration so float appears first in the union, all params are initialized as floats.&lt;/P&gt;&lt;P&gt;I've tried typecasting the assigned values but that doesn't work. Any ideas?&lt;/P&gt;&lt;SPAN&gt;Dave&lt;/SPAN&gt;&lt;PRE&gt;typedef struct {&amp;nbsp; char *Desc;&amp;nbsp;&amp;nbsp;&amp;nbsp; union {&amp;nbsp;&amp;nbsp;&amp;nbsp; int i;&amp;nbsp;&amp;nbsp;&amp;nbsp; float f;&amp;nbsp; } Val;} TestStruct;const struct {&amp;nbsp; char *Desc;&amp;nbsp; TestStruct Param[2];} List[] = {&amp;nbsp;&amp;nbsp;&amp;nbsp; {"ListItem1",&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "param1", 1,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "param2", 2.0},&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {"ListItem2",&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "param1", 3.0,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "param2", 4}&amp;nbsp; };void main(void) {&amp;nbsp; for(;;) {}}&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 08:52:37 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150210#M3547</guid>
      <dc:creator>dp</dc:creator>
      <dc:date>2020-10-29T08:52:37Z</dc:date>
    </item>
    <item>
      <title>Re: CW4.5 S12X Initializing unions</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150211#M3548</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;This is exactly what the standard states about initializing unions: when using an initializer for a union, only values of the same type with the first member of the union are taken into account. For instance:&lt;BR /&gt;&lt;BR /&gt;union myUnion { int i; char* s; };&lt;BR /&gt;&lt;BR /&gt;union myUnion&amp;nbsp; u = { 1 };&amp;nbsp; // ok&lt;BR /&gt;&lt;BR /&gt;union myUnion u1 = { "c"};&amp;nbsp; // not ok&lt;BR /&gt;&lt;BR /&gt;In the second case you will get an error.&lt;BR /&gt;&lt;BR /&gt;Unfortunately you may have to programmatically initialize each member of your array.&lt;BR /&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Sep 2007 22:54:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150211#M3548</guid>
      <dc:creator>Navidad</dc:creator>
      <dc:date>2007-09-26T22:54:49Z</dc:date>
    </item>
    <item>
      <title>Re: CW4.5 S12X Initializing unions</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150212#M3549</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;Since the array is a const in flash I cannot change values with a simple assignement. I was hoping someone&amp;nbsp; knew of a way to do this specifically in CW.&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Sep 2007 00:05:58 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150212#M3549</guid>
      <dc:creator>dp</dc:creator>
      <dc:date>2007-09-27T00:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: CW4.5 S12X Initializing unions</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150213#M3550</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;I'll get rid of the union, declare and initialize as a float, and re-cast to an int in the code when needed.&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 28 Sep 2007 03:59:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150213#M3550</guid>
      <dc:creator>dp</dc:creator>
      <dc:date>2007-09-28T03:59:59Z</dc:date>
    </item>
    <item>
      <title>Re: CW4.5 S12X Initializing unions</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150214#M3551</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;If the values are in the flash, ie they are const, why would it matter which member you initialize? You are obviously going to use all members of the union, otherwise you wouldn't have picked that data type. Simply place the union member you wish to initialize first.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 28 Sep 2007 13:15:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150214#M3551</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2007-09-28T13:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: CW4.5 S12X Initializing unions</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150215#M3552</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;Even though these are consts in flash, the struct defines a format used in an array. Some of the parameters of that struct will be floats, some ints even though they may occupy the same relative position in the the struct. A union would be the perfect way to handle both types until I ran into the initialization problems.&lt;BR /&gt;&lt;BR /&gt;Navidad is correct about the c rules regarding union initializations. Whatever the compiler encounters first will be the type used for the remaining assigments just as in my example above.&lt;BR /&gt;&lt;BR /&gt;The solution of getting rid of the union and declaring floats works fine. I use another element of the struct to tell me whether the value needs to be used as a float or int then re-cast to int as needed in the program.&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 20 Oct 2007 11:32:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150215#M3552</guid>
      <dc:creator>dp</dc:creator>
      <dc:date>2007-10-20T11:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: CW4.5 S12X Initializing unions</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150216#M3553</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;I still don't see any problem here...&lt;BR /&gt;&lt;BR /&gt;typedef union&lt;BR /&gt;{&lt;BR /&gt;struct&lt;BR /&gt;{&lt;BR /&gt;int someInt;&lt;BR /&gt;float someFloat;&lt;BR /&gt;&lt;BR /&gt;/* add more members, nested structs, arrays... */&lt;BR /&gt;&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;SomeOtherStructure a;&lt;BR /&gt;SomeVeryComplicatedStructure b;&lt;BR /&gt;unsigned char bytes[x];&lt;BR /&gt;/* add as many as you like... /*&lt;BR /&gt;&lt;BR /&gt;} SomeUnion;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;const SomeUnion myUnion = {123, 3.14, /*...*/ };&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 22 Oct 2007 20:31:32 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/CW4-5-S12X-Initializing-unions/m-p/150216#M3553</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2007-10-22T20:31:32Z</dc:date>
    </item>
  </channel>
</rss>

