enum-type anonymous structs

A

andreyvul

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

Peter Nilsson

andreyvul said:
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.
 
A

andreyvul

This is not legal C.



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?

 
D

Dan Henry

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?
 
A

andreyvul

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?
 
P

Peter Nilsson

andreyvul said:
I know.


:(

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.
 
D

Dan Henry

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.
 
T

Thad Smith

Dan said:
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
};
 
K

Keith Thompson

andreyvul said:
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++.)
 
J

Jack Klein

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
 
R

Ravishankar S

andreyvul said:
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" */

}
 
R

Ravishankar S

Ravishankar S said:
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" */

}
 
M

Martin Ambuhl

andreyvul said:
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.
 
A

andreyvul

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


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




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.
 
A

andreyvul

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:
...
}
}
 
C

Chris Dollin

andreyvul said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top