473,698 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
16 6442
"andreyvul" <an********@gma il.comwrote in message
news:b2******** *************** ***********@q77 g2000hsh.google groups.com...
On Jan 2, 8:48 pm, Dan Henry <use...@danlhen ry.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)colo r);
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,Rain BColor.BLUE // I think PASCAL has this method.
2) Color::BLUE and RainBColor::Blu e

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

"Ravishanka r S" <ra***********@ in.bosch.comwro te in message
news:fl******** **@news4.fe.int ernet.bosch.com ...
"andreyvul" <an********@gma il.comwrote in message
news:b2******** *************** ***********@q77 g2000hsh.google groups.com...
On Jan 2, 8:48 pm, Dan Henry <use...@danlhen ry.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)colo r);
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,Rain BColor.BLUE // I think PASCAL has this method.
2) Color::BLUE and RainBColor::Blu e

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...@earthl ink.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...@spam cop.netwrote:
On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
<andrey....@gma il.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....@gma il.comwrote:
I used structs so that I could namespace constants, namely error
codes.
To use as cases in a switch(setjmp(b uf)) 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
2157
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 my application (in different classes in different projects)?
21
6342
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 different enum definitions? I think it should have been perfectly valid to do that. Its a stupid limitation to have to define disjoint enum symbol definitions. This is like having to have disjoint member names in structures.
18
11351
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
2237
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 does not want the enumname there anymore. This was not a problem with any previous versions of VS. I could not find any help in any of my programming books or in MSDN. Can anyone explain the reasons to me? Is this behavior to stay? class...
5
2665
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 references the Data layer, but I would like to have the enum exposed to all three. Is there a way to trickle down that enum (Like class inheritance exposes classes) so that it can be accessed from the Business layer and UI layers? My UI layer doesn't...
2
3396
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 and return the name "FirstData"
8
8611
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 create an enum to store the sex. Is there a way that I can create a method in the Enum to convert M to Male and F to Female, or does this method have to exist in another class? Thanks for any help!
0
1300
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 import Enum >>> Weekdays = Enum('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
13
18134
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 detail, the enum is declared as, namespace test{ enum MyEnum{ VALUE1,VALUE2 };
3
12265
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: public enum Days { Sunday };
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9023
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.