473,473 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Create 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 44956
se*****@gmail.com 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.com 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.googlegr oups.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_Keith) 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
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...
8
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...
2
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...
2
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
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
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.