<?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>Dont get it</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/20728/dont-get-it</link><description> 
Please can someone show me a code fragment using the following
code. I think I understand the enum, and the union part. But dont get
how the enum works in conjunction with the union part. Every other
example I have found uses an integer in place of</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Dont get it</title><link>https://community.arm.com/thread/47198?ContentTypeID=1</link><pubDate>Sat, 11 Nov 2006 09:48:48 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1d5533d1-daa5-4e53-a191-3588f06e3885</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;Every other example I have found uses an integer in place of
the enum ...&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
I was taking that to mean that you understood the integer
versions.&lt;/p&gt;

&lt;pre&gt;
#include &amp;lt;stdio.h&amp;gt;

struct taggedunion {
    enum {UNKNOWN, INT, LONG, DOUBLE, POINTER} code;
    union {
        int     i;
        long    l;
        double  d;
        void   *p;
    } u;
};

void print_vr(struct taggedunion *vr /* Ptr to variant record */)
{
    switch (vr-&amp;gt;code) {
    case INT:     printf(&amp;quot;i: %d\n&amp;quot;,  vr-&amp;gt;u.i); break;
    case LONG:    printf(&amp;quot;l: %ld\n&amp;quot;, vr-&amp;gt;u.l); break;
    case DOUBLE:  printf(&amp;quot;d: %lf\n&amp;quot;, vr-&amp;gt;u.d); break;
    case POINTER: printf(&amp;quot;p: %p\n&amp;quot;,  vr-&amp;gt;u.p); break;
    case UNKNOWN:
    default:      printf(&amp;quot;Type unknown\n&amp;quot;);    break;
    }
}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Dont get it</title><link>https://community.arm.com/thread/88020?ContentTypeID=1</link><pubDate>Sat, 11 Nov 2006 03:30:35 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:54c9e453-1593-4cd8-9763-cf43e40075a4</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
An enum is really just a way of assigning symbolic names to
constant integer values; thus&lt;/p&gt;

&lt;pre&gt;
enum {UNKNOWN, INT, LONG, DOUBLE, POINTER}
&lt;/pre&gt;

&lt;p&gt;
is almost equivalent to&lt;/p&gt;

&lt;pre&gt;
#define UNKNOWN 0
#define INT 1
#define LONG 2
#define DOUBLE 3
#define POINTER 4
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
The difference is that enum is part of the &amp;#39;C&amp;#39; language, but #defines
are handled entirely by the preprocessor - so the compiler never gets
to see the symbolic names. One advantage this gives is the
&lt;i&gt;opportunity&lt;/i&gt; for the compiler to include the symbolic names in
its debug info - I don&amp;#39;t know if Keil&amp;#39;s ARM tools take advantage of
this, but C51 doesn&amp;#39;t :-(&lt;/p&gt;

&lt;p&gt;
See &lt;a href="http://c-faq.com/struct/enumvsdefine.html"&gt;c-faq.com/.../enumvsdefine.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
Again, this is standard &amp;#39;C&amp;#39; stuff - nothing specifically to do
with ARM, Keil, or embedded systems.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Dont get it</title><link>https://community.arm.com/thread/47194?ContentTypeID=1</link><pubDate>Sat, 11 Nov 2006 00:12:41 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:aa91da7f-ba30-4345-865e-a829f8644dcc</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
If you understand the other examples using an integer, then you&amp;#39;ll
understand this one using an enum, because an enum &lt;b&gt;is&lt;/b&gt; an
integer type.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>