<html>
<body>
Hi Ira,<br><br>
The answer is NO and YES in that order.<br><br>
No, it's not a compiler bug.<br><br>
Yes, union storage is very compiler specific and very dangerous to use in
the way you have done it. Be aware that there is no specification in C
for how a variable is stored inside a union. The 'C' specification only
guarantees that you will get back what you have written, when using the
same types for both actions. It's possible (but <b>very unlikely</b>)
that the storage format may change between different revisions of the
same compiler and that can lead to disaster.<br><br>
Also your definition of the union is incorrect. In the way you have
defined it the first 'u' is an identifier and not a type, so cannot be
used as a type definition later. I'm not surprised it didn't work. You
should have done it one of the following ways:<br><br>
<font face="Courier New, Courier" color="#0000FF">union u {<br>
unsigned long l;<br>
char c[4];<br>
} ;<br><br>
union u temp, foo;<br><br>
</font>or<br><br>
<font face="Courier New, Courier" color="#0000FF">typedef union {<br>
unsigned long l;<br>
char c[4];<br>
} u;<br><br>
u temp, foo;<br><br>
</font>but not both at the same time.<br><br>
Finally, since 'temp.c' is an 'array of char', you could use 'cstrcpy()'
or 'strcpy()' to fill it and 'strcpy()' to copy it to 'foo.c'. That would
ensure the integrity of the trailing 'NULL' in both cases.<br><br>
<br>
<font face="Courier New, Courier" color="#0000FF"> cstrcpy(temp.c,
"abc"); // Assuming 'String in Flash only' is
checked.<br><br>
</font>or<br><br>
<font face="Courier New, Courier">
</font><font face="Courier New, Courier" color="#0000FF">strcpy(temp.c,
"abc"); // Assuming strings in RAM.<br><br>
</font>then<br><br>
<font face="Courier New, Courier">
</font><font face="Courier New, Courier" color="#0000FF">strcpy(foo.c,
temp.c);<br><br>
</font>HTH.<br><br>
All the best for now,<br>
John<br><br>
<br>
At 17:00 02/08/2007, you wrote:<br>
<blockquote type=cite class=cite cite="">I wrote some code that looks
somewhat like this and it didn't work because, well, let's see the code
first.<br><br>
So my problem turned out to be that temp.c[3] became not empty and adding
"temp.c[3] = 0;" and "foo.c[3] = 0;"<br><br>
I realize that using a union like that is very target and maybe even
compiler specific but it works and it's a very easy way to implement what
I want to do. What I don't understand is why when I declare "u
temp", temp ends up containing random stuff. I thought that I
read somewhere that variables are supposed to be initialized to 0. It
always works right after I program the processor, it never works if I
power the processor on and off and it occasionally works when I reset the
processor. And even when it works, one it stops working, it will
never start working again.<br><br>
It's astonishing how many subtle bugs can fit in a 200 line C
program.<br><br>
union u {<br>
<x-tab> </x-tab>unsigned
long l;<br>
<x-tab> </x-tab>char
c[4];<br>
}<br>
u foo;<br><br>
main(){<br>
<x-tab> </x-tab>unsigned
long i;<br>
<x-tab> </x-tab>i =
65000;<br>
<x-tab> </x-tab>while (1)
{<br>
<x-tab> </x-tab><x-tab>
</x-tab><br>
<x-tab> </x-tab><x-tab>
</x-tab>i =
dosomestuff();<br>
<x-tab> </x-tab><x-tab>
</x-tab>if (i >
foo.l)<br>
<x-tab> </x-tab><x-tab>
</x-tab><x-tab>
</x-tab>
dosomeotherstuff();<br>
<x-tab> </x-tab>}<br>
}<br><br>
void intfunction(){<br>
<x-tab> </x-tab>u
temp;<br>
<x-tab> </x-tab>CLI();<br>
<x-tab> </x-tab>temp.c[0]
= a;<br>
<x-tab> </x-tab>temp.c[1]
= b;<br>
<x-tab> </x-tab>temp.c[2]
= c;<br>
<x-tab> </x-tab>if (temp.l
< 55000){<br>
<x-tab> </x-tab><x-tab>
</x-tab>SEI();<br>
<x-tab> </x-tab><x-tab>
</x-tab>return;<br>
<x-tab> </x-tab>}<br>
<x-tab> </x-tab>foo.c[0] =
temp.c[0];<br>
<x-tab> </x-tab>foo.c[1] =
temp.c[1];<br>
<x-tab> </x-tab>foo.c[2] =
temp.c[2];<br>
<x-tab> </x-tab>SEI();<br>
}<br><br>
_______________________________________________<br>
Icc-avr mailing list<br>
Icc-avr@imagecraft.com<br>
<a href="http://dragonsgate.net/mailman/listinfo/icc-avr" eudora="autourl">
http://dragonsgate.net/mailman/listinfo/icc-avr</a><br><br>
</blockquote></body>
</html>