473,555 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum within structure??

can we define enum within a structure??

for example,
typedef enum {red,blue}color ;

struct s1{
color c;
}obj;

obj.c = blue;

Is it possible?? if yes, can anybody tell me how it possible??

-Sethu

Mar 9 '07 #1
6 44978
se*****@gmail.c om wrote:
can we define enum within a structure??

for example,
typedef enum {red,blue}color ;

struct s1{
color c;
}obj;

obj.c = blue;

Is it possible?? if yes, can anybody tell me how it possible??
Like this?

struct X {
enum Colour { red, blue } colour;
};

int main(void)
{
struct X x;

x.colour = blue;

return 0;
}

--
Ian Collins.
Mar 9 '07 #2
On Mar 8, 11:22 pm, "seth...@gmail. com" <seth...@gmail. comwrote:
can we define enum within a structure??

for example,

typedef enum {red,blue}color ;

struct s1{
color c;

}obj;

obj.c = blue;

Is it possible?? if yes, can anybody tell me how it possible??
Sure. Nothing wrong with that. Are you having a problem? Maybe post
your code.

Mar 9 '07 #3
se*****@gmail.c om wrote:
can we define enum within a structure??

for example,

typedef enum {red,blue}color ;

struct s1{
color c;
}obj;

obj.c = blue;

Is it possible??
Yes.

An alternative is to do away with the typedef and give a name for the
enumeration type.

Mar 9 '07 #4
Thanks to all of u
-Sethu

Mar 9 '07 #5

<se*****@gmail. comwrote in message
news:11******** **************@ c51g2000cwc.goo glegroups.com.. .
can we define enum within a structure??

for example,
typedef enum {red,blue}color ;

struct s1{
color c;
}obj;

obj.c = blue;

Is it possible?? if yes, can anybody tell me how it possible??
It is possible because you can consider the type of an enum as int so it's
no more special than having an int member.

Beware though that if you do something like

struct X
{
enum Colour { red, green } col;
};

The enum Colour is actually *not* an embedded type as it would be in C++ so
the following should still compile

Colour c = green;

This would be an error in C++
Mar 9 '07 #6
"Servé Laurijssen" <se*@n.tkwrites :
[...]
Beware though that if you do something like

struct X
{
enum Colour { red, green } col;
};

The enum Colour is actually *not* an embedded type as it would be in C++ so
the following should still compile

Colour c = green;

This would be an error in C++
It would be an error in C as well, but this:

enum Colour c = green;

would be legal.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
22383
by: Nils Petter Vaskinn | last post by:
I'm using an enum that's declared within a function (since I only need it within that function.) I can't find anything about this in "The C++ Programming Language" by Stroustroup and I don't have the standard. Is this legal? // test.cpp #include <iostream>
8
37260
by: Alex | last post by:
If I have something like: class foo { enum X { a, b, c }; }; Then I'd expect to use the enum this way: X::a . Instead, I have to use foo::a Why is this? doesn't the enum create a 'scope' by itself?
2
3084
by: mrhicks | last post by:
Hello all, I have a question about enumerations. Within some requirements data passed back a certain bit field is defined by three bits then in another section the bit field is defined as 4 bits. The second set is a superset of the first set. The enumeration look as following... enum SET1 { AutoOffState = 0x00, AutoOneState = 0x01,...
2
2446
by: kimimaro | last post by:
hi I wonder if array can be work along with structure? Below are the declaration of my structure struct employee{ char ID; char Name; char Department;
5
1464
by: Kelvin Moss | last post by:
Hi group, I have an array of structure. Say - typedef struct Person { int i; int j; int k; }person;
4
1788
by: imme929 | last post by:
I got things working until I tried adding this enum to a structure... Public Enum Keyboard EnglishUS EnglishUK Spanish German Italian French
5
2568
by: ScottM | last post by:
I expect this has been covered somewhere, but I can't find it. Given: class A { public: A() {...does stuff...} }; struct S { A a; };
0
7622
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...
0
7547
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...
0
7825
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. ...
0
8060
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...
1
7588
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...
1
5452
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5171
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...
0
3575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2037
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

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.