473,219 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

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 6392
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kenneth | last post by:
I want to define an enum for possible languages in my application. My enum look like this: Public Enum Languages English = 0 Portuguese = 1 End Enum How can i reuse this enum in the rest of...
21
by: Bilgehan.Balban | last post by:
Hi, I have two different enum definitions with members of same name. The compiler complains about duplicate definitions. Is this expected behaviour? Can't I have same-named fields in...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
8
by: Craig Klementowski | last post by:
All, I've installed the VS 2005 Beta 1 and was trying to build our current product. I get a compile error when enum value is specified with classname::enumname::enumvalue. Seems the compiler...
5
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer...
2
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
8
by: kc | last post by:
I'm trying to pull data from a database that I have no control over the structure of. There's a members table with a column for the member's sex. It just stores the sex as M or F. I'd like to...
0
by: Ben Finney | last post by:
Howdy all, I've uploaded enum 0.3 to the Cheeseshop. <URL:http://cheeseshop.python.org/pypi/enum/> Enumerations are now sequences, iterable (as before) *and* indexable:: >>> from enum...
13
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little...
3
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.