468,457 Members | 1,598 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

enum-type anonymous structs

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;
}
Jan 3 '08 #1
16 5769
andreyvul <andrey....@gmail.comwrote:
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
Jan 3 '08 #2
On Jan 2, 8:03 pm, Peter Nilsson <ai...@acay.com.auwrote:
andreyvul <andrey....@gmail.comwrote:
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
Jan 3 '08 #3
On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
<an********@gmail.comwrote:
>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
Jan 3 '08 #4
On Jan 2, 8:48 pm, Dan Henry <use...@danlhenry.comwrote:
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?
Jan 3 '08 #5
andreyvul <andrey....@gmail.comwrote:
Peter Nilsson <ai...@acay.com.auwrote:
andreyvul <andrey....@gmail.comwrote:
...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
Jan 3 '08 #6
On Wed, 2 Jan 2008 17:58:29 -0800 (PST), andreyvul
<an********@gmail.comwrote:
>On Jan 2, 8:48 pm, Dan Henry <use...@danlhenry.comwrote:
>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
Jan 3 '08 #7
Dan Henry wrote:
On Wed, 2 Jan 2008 17:58:29 -0800 (PST), andreyvul
<an********@gmail.comwrote:
>On Jan 2, 8:48 pm, Dan Henry <use...@danlhenry.comwrote:
>>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
Jan 3 '08 #8
andreyvul <an********@gmail.comwrites:
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) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 3 '08 #9
On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
<an********@gmail.comwrote 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
Jan 3 '08 #10
"andreyvul" <an********@gmail.comwrote in message
news:b2**********************************@q77g2000 hsh.googlegroups.com...
On Jan 2, 8:48 pm, Dan Henry <use...@danlhenry.comwrote:
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" */

}





Jan 3 '08 #11

"Ravishankar S" <ra***********@in.bosch.comwrote in message
news:fl**********@news4.fe.internet.bosch.com...
"andreyvul" <an********@gmail.comwrote in message
news:b2**********************************@q77g2000 hsh.googlegroups.com...
On Jan 2, 8:48 pm, Dan Henry <use...@danlhenry.comwrote:
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" */

}





Jan 3 '08 #12
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.
Jan 3 '08 #13
On Jan 3, 2:12 am, Martin Ambuhl <mamb...@earthlink.netwrote:
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.
Jan 3 '08 #14
On Jan 2, 11:50 pm, Jack Klein <jackkl...@spamcop.netwrote:
On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
<andrey....@gmail.comwrote 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:
...
}
}
Jan 3 '08 #15
On Jan 3, 12:46 pm, andreyvul <andrey....@gmail.comwrote:
I used structs so that I could namespace constants, namely error
codes.
To use as cases in a switch(setjmp(buf)) statement.
Jan 3 '08 #16
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/

Jan 3 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kenneth | last post: by
21 posts views Thread by Bilgehan.Balban | last post: by
18 posts views Thread by Visual Systems AB \(Martin Arvidsson\) | last post: by
8 posts views Thread by Craig Klementowski | last post: by
5 posts views Thread by Andrea Williams | last post: by
2 posts views Thread by Dennis | last post: by
8 posts views Thread by kc | last post: by
reply views Thread by Ben Finney | last post: by
13 posts views Thread by toton | last post: by
3 posts views Thread by Cmtk Software | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.