Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > enum-type anonymous structs

Reply
Thread Tools

enum-type anonymous structs

 
 
Ravishankar S
Guest
Posts: n/a
 
      01-03-2008
"andreyvul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Jan 2, 8:48 pm, Dan Henry <(E-Mail Removed)> wrote:
> > How does the functionality of enums and structures intersect?

>
> enums are lists of constants, correct?
> What if the list could be accessed using struct format, like
> enum.member?


enums are something like compile time constants in C. But it does not
introduce a
namespace. For example the folowing code gave me errors..

#include <stdio.h>

enum RainBColor {
VIOLET = 0,
INDIGO = 1,
BLUE = 2,
..

};
enum Color {
WHITE = 0;
BLUE = 1;

};
int main(void)
{
enum Color color = BLUE;
printf("color has the integer value = %d\n",(int)color);
return 0;
}

color.c:12: error: conflicting types for `BLUE'
color.c:7: error: previous declaration of `BLUE'

Only way out in C is to use a prefix like RB_BLUE and COL_BLUE. Sure it
would be nice if C had let us
refer the enum consts in someways like

1) Color.BLUE,RainBColor.BLUE // I think PASCAL has this method.
2) Color::BLUE and RainBColor::Blue

It would sure make for readability as clarity.

Suppose we use const structs, we lose the "type info" since we have to refer
to the variable as plain "ints".

typedef const struct {
unsigned char WHITE = 0;
unsigned char BLUE = 1;
..
} Color;

typedef const struct {
unsigned char VIOLET = 0;
unsigned charu INDIGO = 1;
unsigned char BLUE = 2;
..
};

int main(void)
{
unsigned char color = RainBColor.BLUE; /* But type is now "unsigned
char" */

}















 
Reply With Quote
 
 
 
 
Ravishankar S
Guest
Posts: n/a
 
      01-03-2008

"Ravishankar S" <(E-Mail Removed)> wrote in message
news:flhr6q$gd0$(E-Mail Removed)...
> "andreyvul" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On Jan 2, 8:48 pm, Dan Henry <(E-Mail Removed)> wrote:
> > > How does the functionality of enums and structures intersect?

> >
> > enums are lists of constants, correct?
> > What if the list could be accessed using struct format, like
> > enum.member?

>
> enums are something like compile time constants in C. But it does not
> introduce a
> namespace. For example the folowing code gave me errors..
>
> #include <stdio.h>
>
> enum RainBColor {
> VIOLET = 0,
> INDIGO = 1,
> BLUE = 2,
> ..
>
> };
> enum Color {
> WHITE = 0;
> BLUE = 1;
>
> };
> int main(void)
> {
> enum Color color = BLUE;
> printf("color has the integer value = %d\n",(int)color);
> return 0;
> }
>
> color.c:12: error: conflicting types for `BLUE'
> color.c:7: error: previous declaration of `BLUE'
>
> Only way out in C is to use a prefix like RB_BLUE and COL_BLUE. Sure it
> would be nice if C had let us
> refer the enum consts in someways like
>
> 1) Color.BLUE,RainBColor.BLUE // I think PASCAL has this method.
> 2) Color::BLUE and RainBColor::Blue
>
> It would sure make for readability and clarity.
>
> Suppose we use const structs, we lose the "type info" since we have to

refer
> to the variable as plain "ints". It also involves memory for the const

structs..
>
> const struct {
> unsigned char WHITE = 0;
> unsigned char BLUE = 1;
> ..
> } Color;
>
> const struct {
> unsigned char VIOLET = 0;
> unsigned char INDIGO = 1;
> unsigned char BLUE = 2;
> ..
> }RainBColor;
>
> int main(void)
> {
> unsigned char color = RainBColor.BLUE; /* But type is now "unsigned
> char" */
>
> }
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-03-2008
andreyvul wrote:
> If I try compiling this in gcc, it says: "error: request for member
> `baz' in something not a structure or union".


That's strange. My copy of gcc gives a number of messages. See below.

> Any workarounds or tips on how to make a structure such that it
> behaves like an enum but its members can be addressed with '.'?


structs are not enums. It doesn't even make sense for them to "behave
like an enum."

>
>
> static struct {
> static const int baz = 1;
> } bar;
>
> void foo() {
> int x = bar.baz;
> }


Notice the diagnostics gcc gives me for your code:
3: error: expected specifier-qualifier-list before 'static'
4: warning: struct has no members
In function 'foo':
8: error: 'struct <anonymous>' has no member named 'baz'
8: warning: unused variable 'x'

These arise because there is no such thing as a static member in a C
struct. Perhaps you wanted to use some other language.
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      01-03-2008
On Jan 3, 2:12 am, Martin Ambuhl <(E-Mail Removed)> wrote:
> andreyvul wrote:
> > If I try compiling this in gcc, it says: "error: request for member
> > `baz' in something not a structure or union".

>
> That's strange. My copy of gcc gives a number of messages. See below.
>
> > Any workarounds or tips on how to make a structure such that it
> > behaves like an enum but its members can be addressed with '.'?

>
> structs are not enums. It doesn't even make sense for them to "behave
> like an enum."
>
> >
> >

>
> > static struct {
> > static const int baz = 1;
> > } bar;

>
> > void foo() {
> > int x = bar.baz;
> > }

>
> Notice the diagnostics gcc gives me for your code:
> 3: error: expected specifier-qualifier-list before 'static'
> 4: warning: struct has no members
> In function 'foo':
> 8: error: 'struct <anonymous>' has no member named 'baz'
> 8: warning: unused variable 'x'
>
> These arise because there is no such thing as a static member in a C
> struct. Perhaps you wanted to use some other language.


I used structs so that I could namespace constants, namely error
codes.
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      01-03-2008
On Jan 2, 11:50 pm, Jack Klein <(E-Mail Removed)> wrote:
> On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
> <(E-Mail Removed)> wrote in comp.lang.c:
>
> > If I try compiling this in gcc, it says: "error: request for member
> > `baz' in something not a structure or union".
> > Any workarounds or tips on how to make a structure such that it
> > behaves like an enum but its members can be addressed with '.'?
> > I don't want to have to do this using #defines, as it would be far too
> > messy.
> > code:
> > static struct {
> > static const int baz = 1;
> > } bar;

>
> > void foo() {
> > int x = bar.baz;
> > }

>
> A few others have questioned why you want to do it, but I haven't seen
> anyone post a suggestion like this:
>
> struct bar { int baz; };
>
> static const struct bar bar = { 1 };
>
> void foo(void)
> {
> int x = bar.baz;
>
> }
>
> You just need to define the type, and than a const initialized (and
> static, if you want) object of the type separately.


Fails if you do this:
void foo(int x) {
switch (x) {
case bar.baz:
...
}
}
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      01-03-2008
On Jan 3, 12:46 pm, andreyvul <(E-Mail Removed)> wrote:
> I used structs so that I could namespace constants, namely error
> codes.


To use as cases in a switch(setjmp(buf)) statement.
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      01-03-2008
andreyvul wrote:

> I used structs so that I could namespace constants, namely error
> codes.


It has presumably become apparent to you that you can't do
this in C; you're essentially stuck with naming conventions.
Just pick good names and wash thoroughly and you'll be OK.

--
Preprocessor Hedgehog
"Who do you serve, and who do you trust?" /Crusade/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments