<?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>union/struct problem</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/35638/union-struct-problem</link><description> 
Hello, 

 
it works in devcpp (as c project) but not working in Keil. What is
my fault. 
output must be &amp;quot;12345&amp;quot; but it is not. 

 
typedef union {
 u8 Reg8[5];
 struct
 {
 u8 Select;
 u32 Value32;
 }Regs;
}CreditLoadRegs_t;


const char MyArray[]={</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/157932?ContentTypeID=1</link><pubDate>Wed, 15 Jun 2016 07:14:01 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a19a5bf6-c17c-4003-9470-817aabed074c</guid><dc:creator>Kutay Eem</dc:creator><description>&lt;p&gt;&lt;p&gt;
Ok, I understand, Thanks,&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/157894?ContentTypeID=1</link><pubDate>Wed, 15 Jun 2016 00:52:42 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2971ceb1-00f3-406c-bef8-5dfa8d0e347e</guid><dc:creator>edPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
&amp;quot;Because serializing process is slowing for parsing process&amp;quot;&lt;/p&gt;

&lt;p&gt;
You&amp;#39;d be surprised at how fast serializing can be. When your web
browser retrieves a web page, it gets serialized data. When it
processes a JPG image, it&amp;#39;s decoding serialized data. When you view a
BluRay movie, the player decodes serialized data.&lt;/p&gt;

&lt;p&gt;
A &amp;quot;file format&amp;quot; is a protocol for how the data should be
serialized into the file so writer and reader will be able to both
understand the content. All networking happens with serialized data.
It&amp;#39;s just that in some situations, one layer of serialized data may
contain &amp;quot;blobs&amp;quot; of unknown payload, that a different protocol layer
will be able to work on.&lt;/p&gt;

&lt;p&gt;
In the end, you should avoid having transfers or data sharing
contain data blobs where the content is raw memory dumps unless the
data is known to just be a byte stream. It&amp;#39;s a short-term
cost/time-saving that tends to give lots of interesting problems.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/157856?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2016 23:32:13 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:4861baa6-0e5f-4388-b2ee-4c588343c9f0</guid><dc:creator>Westonsupermare Pier</dc:creator><description>&lt;p&gt;&lt;p&gt;
One could presumably use sizeof() and offsetof() to arrive at a
conclusion.&lt;/p&gt;

&lt;p&gt;
I would be careful as it is very easy to get caught with an
alignment fault on ARM devices if you are not paying attention.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/157829?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2016 23:14:06 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:efa07407-3c52-4918-8eff-7a7fabb6bb7a</guid><dc:creator>Kutay Eem</dc:creator><description>&lt;p&gt;&lt;p&gt;
Thank you a lot for sample and information,&lt;/p&gt;

&lt;p&gt;
I had already known serializing but I hadn&amp;#39;t known its technical
name as serializing.&lt;/p&gt;

&lt;p&gt;
I&amp;#39;m using it but usually for big string/data sending/receiving, if
I&amp;#39;ll send or receive little size data which is also combinations of
difference variable type, I&amp;#39;m using packed structs as rx and tx
buffer. (Because serializing process is slowing for parsing
process)&lt;br /&gt;
But if there are big data size, I&amp;#39;m using serializing process.&lt;/p&gt;

&lt;pre&gt;
__packed typedef struct{
  u8    Reg8;
  u32   Reg32;
  float Flt;
  u16   Reg16;
  .....
}Comm_t;

// sender side
Comm_t  SenderCommRegs;

UartCanUsbSenderFonc(Comm_t *cPtr);

UartCanUsbSenderFonc(&amp;amp;SenderCommRegs);

// receiver side
Comm_t  RecvCommRegs;

UartCanUsbRecvFonc(Comm_t *cPtr);

UartCanUsbRecvFonc(&amp;amp;RecvCommRegs);

&lt;/pre&gt;

&lt;p&gt;
in fact, I&amp;#39;m a little confused.&lt;/p&gt;

&lt;p&gt;
I can test it but if I use __packed keyword also, Can the keil
compiler add byte/bytes for padding?&lt;/p&gt;

&lt;p&gt;
if it is not add, The union must cover struct regs. isn&amp;#39;t it?&lt;/p&gt;

&lt;pre&gt;
typedef union{
   u8 Areg8[5];       // 5 bytes u8 array
   __packed struct{
       u8  Temp8;     // 1 byte
       u16 Low16;     // 2 bytes
       u16 Hi16;      // 2 bytes
   }Regs;
}x_t;
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/150927?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2016 14:31:36 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0e1cb219-b8d3-4437-a1f7-cbbb5be41225</guid><dc:creator>edPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
A common way of serializing data is to write code that takes a
uint8_t pointer and inserts or retrieves different data types
byte-by-byte in a well-defined order.&lt;/p&gt;

&lt;p&gt;
So if serializing a int32_t, the serialize function may emit it
from high-to-low byte. And the deserialize function would extract and
join from high-to-low. That means that the byte stream transmitted
will be compatible even if the sender and receiver have different
byte order.&lt;/p&gt;

&lt;p&gt;
And it also means that it doesn&amp;#39;t matter if the sender and
receiver have different rules for padding between fields in a
struct.&lt;/p&gt;

&lt;p&gt;
So you might do:&lt;/p&gt;

&lt;pre&gt;
p = transmit_buffer;
p = pack_u32(p,my_struct.my_u32);
p = pack_u32(p,my_struct.other_u32);
p = pack_u16(p,my_struct.some_u16);
p = pack_string(p,my_struct.string,sizeof(my_struct.string));
...
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
and on the other side do something like:&lt;/p&gt;

&lt;pre&gt;
p = receive_buffer;
p = extract_u32(p,&amp;amp;my_struct.my_u32);
p = extract_u32(p,&amp;amp;my_struct.other_u32);
p = extract_u16(p,&amp;amp;my_struct.some_u16);
p = extract_string(p,my_struct.string,sizeof(my_struct.string));
...
&lt;/pre&gt;

&lt;p&gt;
Or maybe switch parameter/return value and do:&lt;/p&gt;

&lt;pre&gt;
p = receive_buffer;
my_struct.my_u32 = extract_u32(&amp;amp;p);
my_struct.other_u32 = extract_u16(&amp;amp;p);
...
&lt;/pre&gt;

&lt;p&gt;
This is a situation where C++ is very handy, since it can keep
track of the size of the transmit buffer and fail if trying to insert
more than what fits. Or keep track of the number of available bytes
in the receive buffer and fail if the code tries to extract more than
was available.&lt;/p&gt;

&lt;p&gt;
With C, you need additional parameters or global variables to keep
track of all the state information when packing/unpacking.&lt;/p&gt;

&lt;p&gt;
But with explicit serializing, you can get very robust code that
works with different architectures, compilers, optimization levels,
computer languages etc. The two sides can completely change order and
data types of the internal data as long as they both agree on the
order/size/type of the individual values stored in the serialized
data.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/145706?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2016 13:18:00 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1fe098ce-4835-4a2c-ab1b-24c5c0f78062</guid><dc:creator>Kutay Eem</dc:creator><description>&lt;p&gt;&lt;p&gt;
Dear Hans-Bernhard Broeker&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;&lt;br /&gt;
No. It doesn&amp;#39;t have to be packed. The job at hand can and should be
done entirely without any packed structs, unions and fishy pointer
casts. You&amp;#39;ll want to look up the term &amp;quot;serialization&amp;quot;.&lt;br /&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Thanks your response,&lt;/p&gt;

&lt;p&gt;
Sorry, I didn&amp;#39;t understand why it doesn&amp;#39;t have to packed exactly.
I have read a few books about standart C , embedded C and data
structers in C. But I never read/see about serialization which is not
packed or alligned memory in mcus.&lt;/p&gt;

&lt;p&gt;
I&amp;#39;m asking for learning,&lt;/p&gt;

&lt;p&gt;
if possible, can you give me a small sample or/and can you
show/prefer me a source/book about this subject.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/143686?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2016 10:39:48 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a6a43583-ef9a-437a-9f57-bd24902cf72b</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;But I thought that union make it pack.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
In case of doubt, and definitely in case of surprising behaviour
like the one discussed here, you really should replace such thought
by actually looking up documentation.&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;So the structer must be __packed&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
No. It doesn&amp;#39;t &lt;b&gt;have to&lt;/b&gt; be packed. The job at hand can and
should be done entirely without any packed structs, unions and fishy
pointer casts. You&amp;#39;ll want to look up the term &amp;quot;serialization&amp;quot;.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/145705?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2016 03:10:21 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:989efca5-310f-4f2b-82d6-3dd46b138c45</guid><dc:creator>Kutay Eem</dc:creator><description>&lt;p&gt;&lt;p&gt;
you&amp;#39;re quite right, thanks a lot&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/143685?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2016 02:20:18 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e0e45309-b769-4f17-88e1-e9fa1831174c</guid><dc:creator>edPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
The union is only about overlaying data - not packing data.&lt;/p&gt;

&lt;p&gt;
And it isn&amp;#39;t intended to overlay as a way to type-convert, but to
allow conditional existence of data from a time when RAM was at a
premium.&lt;/p&gt;

&lt;p&gt;
Any and all type conversion performed using unions are a kind of
abuse. When done correctly, it can in some limited cases be used in a
&amp;quot;correct&amp;quot; way without undefined behavior. But it is a slippery slope
- the language standard didn&amp;#39;t intend you to write to one member of
the union and read out the data using a different member of the
union.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/140960?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2016 01:29:43 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:82f2b351-4c65-4447-91e0-5e16e275d711</guid><dc:creator>Kutay Eem</dc:creator><description>&lt;p&gt;&lt;pre&gt;
struct {
    u8      Select;    // 1 byte
    u32     Value32;   // 4 byte
} Regs;
&lt;/pre&gt;

&lt;p&gt;
You&amp;#39;re right. This structer is not packed. But I thought that
union make it pack.&lt;/p&gt;

&lt;p&gt;
Result, I think as following&lt;/p&gt;

&lt;pre&gt;
struct
    {
        u8      Select;    // 1 byte
        u8      unused[3];
        u32     Value32;   // 4 byte
    }Regs;

Reg8[0]=Select
Reg8[1]=unused[0]
Reg8[2]=unused[1]
Reg8[3]=unused[2]
Reg8[4]=LSB_Value32;
&lt;/pre&gt;

&lt;p&gt;
So the structer must be __packed&lt;/p&gt;

&lt;pre&gt;
typedef union {
    u8      Reg8[5];
    __packed struct
    {
        u8      Select;    // 1 byte
        u32     Value32;   // 4 byte
    }Regs;
}CreditLoadRegs_t;
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/137348?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2016 00:59:06 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:95d1e53f-55cb-4d48-b0cb-03b98d97d4d9</guid><dc:creator>edPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
A byte array is packed.&lt;/p&gt;

&lt;p&gt;
But this most definitely is not a byte array:&lt;/p&gt;

&lt;pre&gt;
struct {
    u8      Select;    // 1 byte
    u32     Value32;   // 4 byte
} Regs;
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Value32 would want 32-bit align.&lt;br /&gt;
So you would end up with:&lt;/p&gt;

&lt;pre&gt;
struct {
    u8      Select;    // 1 byte
    u8      pad1[3];
    u32     Value32;   // 4 byte
} Regs;
&lt;/pre&gt;

&lt;p&gt;
Now map that in your union, and you&amp;#39;ll notice that you have 3
bytes of your byte array that maps to air.&lt;/p&gt;

&lt;p&gt;
Next thing - if your byte array is packed from a data structure
that has a different byte order compared to what your processor uses,
then even the struct was packed you would still get the wrong content
seen in Value32.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/129234?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2016 23:55:45 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:27b8667b-d3a5-4eb3-b8e7-e1a735718179</guid><dc:creator>Kutay Eem</dc:creator><description>&lt;p&gt;&lt;p&gt;
Dear Per Westermark,&lt;/p&gt;

&lt;p&gt;
Thanks for your reply,&lt;/p&gt;

&lt;p&gt;
I don&amp;#39;t think the keil compiler does something incorrect. Keil is
best compiler for arm mcus. I&amp;#39;m just trying to find the my fault.&lt;/p&gt;

&lt;p&gt;
&amp;quot;Because of union is packed&amp;quot;&lt;br /&gt;
&amp;quot; What makes you think/say this? &amp;quot;&lt;/p&gt;

&lt;p&gt;
I don&amp;#39;t use &amp;quot;#pragma pack(push,1)&amp;quot; or __packed keywords for pack
process but Are byte arrays already packed? The union includes byte
array so I&amp;#39;m thinking that it is packed.&lt;br /&gt;
I have used many times differently in keil before and I didn&amp;#39;t see
not packed&lt;/p&gt;

&lt;p&gt;
for example, below,&lt;/p&gt;

&lt;pre&gt;

typedef u8  unsigned char;
typedef u32 unsigned int;

u8 Buf[]={1,2,3,4,5,0};

typedef union {
    u8      Reg8[5];
    struct
    {
        u8      Select;    // 1 byte
        u32     Value32;   // 4 byte
    }Regs;
}CreditLoadRegs_t;




CreditLoadRegs_t *Ptr = (CreditLoadRegs_t *)Buf;


Ptr-&amp;gt;Reg8[0] = Ptr-&amp;gt;Select = 1

Ptr-&amp;gt;Value32 = Ptr-&amp;gt;Reg8[1] | (Ptr-&amp;gt;Reg8[2] &amp;lt;&amp;lt; 8) |
               (Ptr-&amp;gt;Reg8[3] &amp;lt;&amp;lt; 16) | (Ptr-&amp;gt;Reg8[4] &amp;lt;&amp;lt; 24)

             = 0x5432

&lt;/pre&gt;

&lt;p&gt;
Am I thinking wrong? if It is wrong, why?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/120795?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2016 22:48:45 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ff339888-b5c6-4f0b-aaf3-ed916aa1b1ac</guid><dc:creator>edPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
&amp;quot;Because of union is packed&amp;quot;&lt;/p&gt;

&lt;p&gt;
What makes you think/say this?&lt;/p&gt;

&lt;p&gt;
You seems to think the Keil compiler does something incorrect. But
doing things that has undefined outcome means it&amp;#39;s expected that you
might get different results on different architectures or when using
different compilers. So code that &amp;quot;works&amp;quot; on one system isn&amp;#39;t
automatically correct code even on the system where it &amp;quot;works&amp;quot;.&lt;/p&gt;

&lt;p&gt;
If you have two architectures with different byte order, then that
32-bit value can print completely differently. You are claiming the
data is sent over the network - so the data isn&amp;#39;t from a different
part of the same program.&lt;/p&gt;

&lt;p&gt;
And different architectures have different alignment requirements,
which means a union or struct would have different rules for the
addition of padding. Another thing here is that on a target with
32-bit align, a union with a 32-bit member value would need to have
32-bit align of the union itself - but that would be incompatible
with typecasts from pointers to 8-bit data, unless you have
explicitly aligned the 8-bit data.&lt;/p&gt;

&lt;p&gt;
Any time you move data between two devices, you shouldn&amp;#39;t rely
like that on the two devices having identical align and byte order.
So programs doing networking have special functions for converting
to/from &amp;quot;network byte order&amp;quot;. Even if you have written the code for
both the sender and the receiver, you should still add proper
pack/unpack of the data, to allow you to change compiler and/or
architecture on one side and stay compatible with the other side.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/108806?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2016 22:12:40 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b83fe356-9d0f-4004-8dc8-8f28cf35db9e</guid><dc:creator>Kutay Eem</dc:creator><description>&lt;p&gt;&lt;p&gt;
And when data came via gprs in BufPtr, I printed it, Data which is
in buffer was as expected.&lt;br /&gt;
But output of typedef union was not. (as if union parser is not
packed.)&lt;/p&gt;

&lt;p&gt;
for example,&lt;/p&gt;

&lt;pre&gt;

u8 GprsBuf[1024]={1,2,3,4,5,...};

u8 *BufPtr = GprsBuf;

for(Cnt=0;Cnt&amp;lt;5;Cnt++)
    printf(&amp;quot;%c&amp;quot;,BufPtr[Cnt]+0x30); // output = &amp;quot;12345&amp;quot;

// if I use as following

CreditLoadRegs_t  *Ptr32 = (CreditLoadRegs_t  *)BufPtr;

for(Cnt=0;Cnt&amp;lt;5;Cnt++)
    printf(&amp;quot;%c&amp;quot;,Ptr32.Reg8[Cnt]+0x30); // output is wrong


&lt;/pre&gt;

&lt;p&gt;
Do you have any idea?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/83208?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2016 14:14:50 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:08d83e25-fdfe-4ca2-b137-74aba29f26c9</guid><dc:creator>Kutay Eem</dc:creator><description>&lt;p&gt;&lt;p&gt;
if you&amp;#39;re writing about my first message, you&amp;#39;re right.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/64799?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2016 14:03:22 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e11209bc-dee9-4ef6-b209-ced8178a1720</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;What is my fault.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Mainly that you&amp;#39;re trying to run a 400 meter race before you&amp;#39;ve
fully mastered walking.&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;output must be &amp;quot;12345&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Based on what promises made by the C programming language&amp;#39;s syntax
and semantics, and/or compiler documention, did you arrive at that
belief?&lt;/p&gt;

&lt;p&gt;
According to my knowledge about those documents, this code causes
&lt;b&gt;undefined behaviour&lt;/b&gt; for two reasons: 1) bad pointer
gymnastics, 2) abuse of a union.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/83209?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2016 13:58:53 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1e85bd19-1cd6-4eb3-a3de-a2da46ba5069</guid><dc:creator>Kutay Eem</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hello,&lt;/p&gt;

&lt;p&gt;
Thx for your reply,&lt;/p&gt;

&lt;p&gt;
&amp;quot;Returning for main() goes where?&amp;quot;&lt;/p&gt;

&lt;p&gt;
Sorry, the code is not original code, just similar. So I don&amp;#39;t use
the function in main.&lt;/p&gt;

&lt;p&gt;
This definition includes null char, MyArray has six bytes but not
important.&lt;br /&gt;
const char MyArray[]={&amp;quot;12345&amp;quot;};&lt;/p&gt;

&lt;p&gt;
because my sample code is wrong. I&amp;#39;m not using printf in original
code, I&amp;#39;m using union pointer memory map. Namely as following&lt;/p&gt;

&lt;p&gt;
// BufPtr which is usart array includes 5 bytes(it is coming with
gprs) so I point with&lt;br /&gt;
// pointer of CreditLoadRegs_t&lt;br /&gt;
// if BufPtr includes &amp;quot;12345&amp;quot;, Result must be Ptr32-&amp;gt;Select=&amp;#39;1&amp;#39;;
and Ptr32-&amp;gt;Value32=&amp;quot;5432&amp;quot;;&lt;br /&gt;
// Because of union is packed&lt;/p&gt;

&lt;p&gt;
This code works in devcpp but don&amp;#39;t work in keil.&lt;/p&gt;

&lt;p&gt;
micro is stm32f103, it can be packed as byte and union is
packed.&lt;/p&gt;

&lt;p&gt;
I didn&amp;#39;t find my fault.&lt;/p&gt;

&lt;pre&gt;

void CreditProc(u8 RW, char *BufPtr)
{
   if (RW == dREAD)
   {
      // some codes
   }else{
        CreditLoadRegs_t  *Ptr32 = (CreditLoadRegs_t  *)BufPtr;
        // I&amp;#39;m using regs here
        Ptr32-&amp;gt;Select &amp;amp;=0x03;
        switch(Ptr32-&amp;gt;Select)
        {
           case dSel0:
               // something
           break;
           /// ...
        }
   }
}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: union/struct problem</title><link>https://community.arm.com/thread/64798?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2016 08:28:57 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:9aef1716-806a-48ce-ac71-c91d2c07c79d</guid><dc:creator>Westonsupermare Pier</dc:creator><description>&lt;p&gt;&lt;p&gt;
C strings require a terminating NUL character, &amp;quot;12345&amp;quot; doesn&amp;#39;t fit
in 5 characters, and printf() is going to malfunction if the NUL is
missing.&lt;/p&gt;

&lt;p&gt;
The code as presented might work.&lt;/p&gt;

&lt;p&gt;
If you don&amp;#39;t get &amp;quot;12345&amp;quot; *what* do you actually get, this might be
far more helpful information than what is expected.&lt;/p&gt;

&lt;p&gt;
Returning for main() goes where?&lt;/p&gt;

&lt;p&gt;
If you don&amp;#39;t provide retargeting/semihosting the printf() will
likely fault due to lack of supporting code.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>