Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

enum-type anonymous structs

 
 
andreyvul
Guest
Posts: n/a
 
      01-03-2008
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;
}
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      01-03-2008
andreyvul <andrey....@gmail.com> wrote:
> 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 '.'?


No.

> 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;


This is not legal C.

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


C++ probably has the feature you're looking for.

Judicious naming conventions is as close as you'll come
in C.

--
Peter
 
Reply With Quote
 
 
 
 
andreyvul
Guest
Posts: n/a
 
      01-03-2008
On Jan 2, 8:03 pm, Peter Nilsson <ai...@acay.com.au> wrote:
> andreyvul <andrey....@gmail.com> wrote:
> > 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 '.'?

>
> No.
>
> > 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;

>
> This is not legal C.
>
> > } bar;

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

>
> C++ probably has the feature you're looking for.

I know.
> Judicious naming conventions is as close as you'll come
> in C.



So I'm stuck to #defines, then?


> --
> Peter


 
Reply With Quote
 
Dan Henry
Guest
Posts: n/a
 
      01-03-2008
On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
<> wrote:

>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;
>}


How does the functionality of enums and structures intersect?

--
Dan Henry
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      01-03-2008
On Jan 2, 8:48 pm, Dan Henry <use...@danlhenry.com> 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?


 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      01-03-2008
andreyvul <andrey....@gmail.com> wrote:
> Peter Nilsson <ai...@acay.com.au> wrote:
> > andreyvul <andrey....@gmail.com> wrote:
> > > ...Any workarounds or tips on how to make a structure
> > > such that it behaves like an enum but its members can
> > > be addressed with '.'?

> >
> > No.
> >
> > > 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;

> >
> > This is not legal C.
> >
> > > } bar;
> > >
> > > void foo() {
> > > * * int x = bar.baz;
> > > }

> >
> > C++ probably has the feature you're looking for.

>
> I know.
>
> > Judicious naming conventions is as close as you'll come
> > in C.

>
>
>
> So I'm stuck to #defines, then?


Unlike your #define (which I'm not sure how you see it
operating), naming conventions is established practice,
so it's easier to maintain.

Perhaps if you explain the real problem, in partiuclar why
you need a static member of a struct, then we can suggest
better alternatives.

--
Peter
 
Reply With Quote
 
Dan Henry
Guest
Posts: n/a
 
      01-03-2008
On Wed, 2 Jan 2008 17:58:29 -0800 (PST), andreyvul
<> wrote:

>On Jan 2, 8:48 pm, Dan Henry <use...@danlhenry.com> 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?
>

Well you've got me there. What if? I don't appreciate the value of
accessing "the enum list" using struct (plus member?) format. More
likely is that I don't understand the problem you are trying to solve.
Quite likely is that C does not, other than possibly by preprocessing
means, provide what you are looking for.

--
Dan Henry
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      01-03-2008
Dan Henry wrote:
> On Wed, 2 Jan 2008 17:58:29 -0800 (PST), andreyvul
> <> wrote:
>
>> On Jan 2, 8:48 pm, Dan Henry <use...@danlhenry.com> 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?
>>

> Well you've got me there. What if? I don't appreciate the value of
> accessing "the enum list" using struct (plus member?) format. More
> likely is that I don't understand the problem you are trying to solve.


I suspect that the OP is trying to introduce a name space so that he can
have different flavors of foo by using the enclosing struct name.

What I do is to prefix enumeration constants for an abbreviation associated
with the category:

enum bar {
BAR_FOO,
BAR_BAZ
};

--
Thad
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-03-2008
andreyvul <> writes:
> 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;
> }


Here's what I get with gcc 4.1.3:

c.c:2: error: expected specifier-qualifier-list before 'static'
c.c: In function 'foo':
c.c:6: error: 'struct <anonymous>' has no member named 'baz'

Are you sure you didn't get an error message on the declaration of
"baz"? (I got no errors when I compiled it as C++.)

--
Keith Thompson (The_Other_Keith) <kst->
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      01-03-2008
On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
<> 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57