473,406 Members | 2,467 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,406 software developers and data experts.

Enum problem...

Is there any way by which I can find number of elements in an enum
list?
Generally I use one last element at the end to find out total number
of elements. e.g.
typedef enum{
SUN,
MON,
TUE,
EndOfList
}weekdays;

But I am facing a situation in which I need to know number of elements
in an enum, which do not have
EndofList as its last element. For backward compatibility reasons I
can not change enum declaration which and add extra element.

In case of arrays I can use sizeof(array)/sizeof(array element
type)....But that wont work here....

Anybody has a solution??

Rohit
Sep 18 '08 #1
13 4765
Rohit wrote:
Is there any way by which I can find number of elements in an enum
list?
Not from within the code, no. (Do you mean "number of elements" or
"value of last element" or "maximum value of elements"? They can all
be different [1].)
Anybody has a solution??
Write a program (or script) which reads the enum definition and counts
the number of elements, then writes out a fragment of C code to
include, or fills values from a template.

As usual, if you tell us more about the problem, we may be able to
provide a better solution.

[1] enum dubious
{
iffy = 128,
hinky = 1
};

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Sep 18 '08 #2
Rohit wrote:
>
Is there any way by which I can find number of elements in an
enum list? Generally I use one last element at the end to find
out total number of elements. e.g.
typedef enum {
SUN,
MON,
TUE,
EndOfList
} weekdays;

But I am facing a situation in which I need to know number of
elements in an enum, which do not have EndofList as its last
element. For backward compatibility reasons I can not change
enum declaration which and add extra element.
If you don't use the '=' operator in the declaration, the value of
the last enumerated item + 1 is the count of items. In the above,
that would be:

1 + EndOfList

so it is advisable to write the declaration in such a manner that
you can easily add to it without changing the last item. E.g:

typedef enum { SUN
,MON
,TUE
,EndOfList} weekdays;

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 18 '08 #3
On Sep 18, 5:33*pm, Chris Dollin <chris.dol...@hp.comwrote:
Rohit wrote:
Is there any way by which I can find number of elements in an enum
list?

Not from within the code, no. (Do you mean "number of elements" or
"value of last element" or "maximum value of elements"? They can all
be different [1].)
OK, here I mean number of elements. I intend to use "number of
elements" in an array declaration, so that array always has same
number of element as there are elements in the enum in question.
From your answer I suppose its not possible to do this. But still I
feel some way should be there, coz it seems to be such an obvious
problem.
>
Anybody has a solution??

Write a program (or script) which reads the enum definition and counts
the number of elements, then writes out a fragment of C code to
include, or fills values from a template.
Frankly speaking I did not catch it. Reading the enum definition and
counting number of elements ????
How can you read a enum definition. Its not an array that you can
parse through. Please elaborate on it.
As usual, if you tell us more about the problem, we may be able to
provide a better solution.

[1] enum dubious
* * {
* * * iffy = 128,
* * * hinky = 1
* * };

--
'It changed the future .. and it changed us.' * * * * * * */Babylon 5/

Hewlett-Packard Limited registered office: * * * * * * * *Cain Road, Bracknell,
registered no: 690597 England * * * * * * * * * * ** * * * * * *Berks RG12 1HN

Rohit
Sep 18 '08 #4
Rohit wrote:
On Sep 18, 5:33*pm, Chris Dollin <chris.dol...@hp.comwrote:
>Rohit wrote:
Is there any way by which I can find number of elements in an enum
list?

Not from within the code, no. (Do you mean "number of elements" or
"value of last element" or "maximum value of elements"? They can all
be different [1].)
OK, here I mean number of elements.
OK.
I intend to use "number of
elements" in an array declaration, so that array always has same
number of element as there are elements in the enum in question.
The number of named values, yes. (Note that without additional
information, that none of the enums are defined using the `= constant`
syntax, you don't know if you can use the enum values as indexes
into that array. How you cope with that depends on what you're
actually trying to do, which I still don't know.)
From your answer I suppose its not possible to do this.
Not from inside the code using Standard features.
But still I feel some way should be there,
Life is not perfect.
coz it seems to be such an obvious problem.
It doesn't happen often enough to matter, I suppose, since
there are work-arounds of various kinds.
Anybody has a solution??

Write a program (or script) which reads the enum definition and counts
the number of elements, then writes out a fragment of C code to
include, or fills values from a template.
Frankly speaking I did not catch it. Reading the enum definition and
counting number of elements ????
Yes.
How can you read a enum definition. Its not an array that you can
parse through.
No, but it will be the content of a file you can read. You don't
have to write a script-or-program that reads C in its full glory,
only something that reads the enum declarations that you are
interested in.

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Sep 18 '08 #5
On Sep 18, 12:10*pm, Chris Dollin <chris.dol...@hp.comwrote:
Rohit wrote:
How can you read a enum definition. Its not an array that you can
parse through.

No, but it will be the content of a file you can read. You don't
have to write a script-or-program that reads C in its full glory,
only something that reads the enum declarations that you are
interested in.
Like this, only much much better:

/* enum.c */
#include <stdio.h>

typedef enum {
MON,
TUE,
WED } weekdays;

int main( int argc, char* argv[] ) {
printf("%d\n", NUMDAYS);
return 0;
}
/* enum.c */

cat enum.c | tr '\n' ' ' | sed "s/;/;\n/g" | sed "s/enum/\nenum/g" |
grep enum | grep weekdays | sed 's/[^{]*{\([^}]*\).*/\1/g' | sed "s/
[^,]//g" | wc -c

Then your build would define it at compile time like:
gcc -DNUMDAYS=the_val_you_calculated -c enum.c

Adam
Sep 18 '08 #6
Rohit wrote:
Is there any way by which I can find number of elements in an enum
list?
Generally I use one last element at the end to find out total number
of elements. e.g.
typedef enum{
SUN,
MON,
TUE,
EndOfList
}weekdays;

But I am facing a situation in which I need to know number of elements
in an enum, which do not have
EndofList as its last element. For backward compatibility reasons I
can not change enum declaration which and add extra element.
What is the underlying problem that you are trying to solve? Maybe there
is a simpler solution.
August
Sep 18 '08 #7
Rohit wrote:
On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@hp.comwrote:
>Rohit wrote:
>>Is there any way by which I can find number of elements in an enum
list?
Not from within the code, no. (Do you mean "number of elements" or
"value of last element" or "maximum value of elements"? They can all
be different [1].)
OK, here I mean number of elements. I intend to use "number of
elements" in an array declaration, so that array always has same
number of element as there are elements in the enum in question.
From your answer I suppose its not possible to do this. But still I
feel some way should be there, coz it seems to be such an obvious
problem.
Actually, it isn't. When you use enumerations properly, there's seldom a
need to know how many elements there are in an enumeration. If you could
explain why you think you need that information, I suspect that we can
suggest a way to deal with the problem; it might involve re-designing
your code.
Sep 18 '08 #8
James Kuyper <ja*********@verizon.netwrites:
Rohit wrote:
>On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@hp.comwrote:
>>Rohit wrote:
Is there any way by which I can find number of elements in an enum
list?
Not from within the code, no. (Do you mean "number of elements" or
"value of last element" or "maximum value of elements"? They can all
be different [1].)
OK, here I mean number of elements. I intend to use "number of
elements" in an array declaration, so that array always has same
number of element as there are elements in the enum in question.
From your answer I suppose its not possible to do this. But still I
feel some way should be there, coz it seems to be such an obvious
problem.

Actually, it isn't. When you use enumerations properly, there's seldom
a need to know how many elements there are in an enumeration. If you
could explain why you think you need that information, I suspect that
we can suggest a way to deal with the problem; it might involve
re-designing your code.
In the case described by the original poster, the usual purpose is to
use the enumeration values to index into the array. In this case,
what is really wanted is, indeed, the maximum value of the elements
(plus one...). I think that the original poster needs to explain how
the problem at hand is different than this common case, because
someone (quite possibly me) is clearly having trouble understanding
the underlying situation.

--
Lowell Gilbert, embedded/networking software engineer
http://be-well.ilk.org/~lowell/
Sep 18 '08 #9
On 18 Sep, 12:57, Rohit <papakap...@gmail.comwrote:
Is there any way by which I can find number of elements in an enum
list?
Generally I use one last element at the end to find out total number
of elements. e.g.
typedef enum{
SUN,
MON,
TUE,
EndOfList

}weekdays;

But I am facing a situation in which I need to know number of elements
in an enum, which do not have
EndofList as its last element. For backward compatibility reasons I
can not change enum declaration which and add extra element.
If "backward compatibility reasons" means "it's in a file which *must
not*, repeat *must not*, be changed, ever - then surely all you need
to do is write code that is compatible with that file?

If the enum might get changed, why can't you change it?
Sep 18 '08 #10
On Thu, 18 Sep 2008 04:57:52 -0700 (PDT), Rohit <pa********@gmail.com>
wrote in comp.lang.c:
Is there any way by which I can find number of elements in an enum
list?
Generally I use one last element at the end to find out total number
of elements. e.g.
typedef enum{
SUN,
MON,
TUE,
EndOfList
}weekdays;

But I am facing a situation in which I need to know number of elements
in an enum, which do not have
EndofList as its last element. For backward compatibility reasons I
can not change enum declaration which and add extra element.
There are _usually_, but not always, ways around compatibility
problems.

If "backwards compatibility reasons" means that you literally cannot
physically modify the source code file at all, then the following will
not help you. But if "backwards compatibility reasons" only means
that you cannot change the definition of the enumeration type as SEEN
by already existing source file, try this:

typedef enum{ /* existing line */
SUN, /* existing line */
MON, /* existing line */
TUE /* existing line */
#ifdef ALLOW_TOTAL_VALUE /* new line 1 */
, EndOfList /* new line 2 */
#endif /* new line 3 */
} weekdays; /* existing line */

....then in your new source files that need the total, you define
ALLOW_TOTAL_VALUE before including the header. The legacy source does
not define the macro, and therefore does not see the new member.
In case of arrays I can use sizeof(array)/sizeof(array element
type)....But that wont work here....

Anybody has a solution??
Another, somewhat messier solution involves not modifying the original
header at all, if that is absolutely forbidden.

Copy the contents of the file to another file, with a different but
similar name. Make your additions to the enum declaration in that
file, leaving all the other contents the same. In your code, include
the new variation.

This gets messy if the original header is ever updated, but perhaps
for "backwards compatibility reasons" that won't happen often, or at
all.

You could actually write a little program in C, Perl, or whatever,
that automatically makes the modified copy of the original header, and
your build system could automatically run this program if the original
header is changed.

There's almost always a solution if you're willing to think "outside
the box", an expression I truly dislike.

--
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
Sep 19 '08 #11

"Lowell Gilbert" <lg******@be-well.ilk.orgwrote in message
news:44************@be-well.ilk.org...
James Kuyper <ja*********@verizon.netwrites:
>Rohit wrote:
>>On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@hp.comwrote:
Rohit wrote:
Is there any way by which I can find number of elements in an enum
list?
In the case described by the original poster, the usual purpose is to
use the enumeration values to index into the array. In this case,
what is really wanted is, indeed, the maximum value of the elements
(plus one...). I think that the original poster needs to explain how
the problem at hand is different than this common case, because
someone (quite possibly me) is clearly having trouble understanding
the underlying situation.
OK, so how do you get the maximum value of the elements, plus one?

In a way that is independent of the source code for the enum (eg. weekdays)
being changed?

Example:

enum (red,green,blue,yellow} colours;

Imported into another source file:

#include "colours.h"

#define NOCOLOURS (yellow+1)
int a[NOCOLOURS];
for (i=0; i<NCOLOURS; ++i) ...

This will break as soon as more colours are added to the enum.

--
Bartc

Sep 19 '08 #12
Bartc wrote:
>
"Lowell Gilbert" <lg******@be-well.ilk.orgwrote in message
news:44************@be-well.ilk.org...
>James Kuyper <ja*********@verizon.netwrites:
>>Rohit wrote:
On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@hp.comwrote:
Rohit wrote:
>Is there any way by which I can find number of elements in an enum
>list?
>In the case described by the original poster, the usual purpose is to
use the enumeration values to index into the array. In this case,
what is really wanted is, indeed, the maximum value of the elements
(plus one...). I think that the original poster needs to explain how
the problem at hand is different than this common case, because
someone (quite possibly me) is clearly having trouble understanding
the underlying situation.

OK, so how do you get the maximum value of the elements, plus one?

In a way that is independent of the source code for the enum (eg.
weekdays) being changed?
You can't.

--
Ian Collins.
Sep 19 '08 #13
"Bartc" <bc@freeuk.comwrites:
"Lowell Gilbert" <lg******@be-well.ilk.orgwrote in message
news:44************@be-well.ilk.org...
>James Kuyper <ja*********@verizon.netwrites:
>>Rohit wrote:
On Sep 18, 5:33 pm, Chris Dollin <chris.dol...@hp.comwrote:
Rohit wrote:
>Is there any way by which I can find number of elements in an enum
>list?
>In the case described by the original poster, the usual purpose is to
use the enumeration values to index into the array. In this case,
what is really wanted is, indeed, the maximum value of the elements
(plus one...). I think that the original poster needs to explain how
the problem at hand is different than this common case, because
someone (quite possibly me) is clearly having trouble understanding
the underlying situation.

OK, so how do you get the maximum value of the elements, plus one?

In a way that is independent of the source code for the enum
(eg. weekdays) being changed?

Example:

enum (red,green,blue,yellow} colours;

Imported into another source file:

#include "colours.h"

#define NOCOLOURS (yellow+1)
int a[NOCOLOURS];
for (i=0; i<NCOLOURS; ++i) ...

This will break as soon as more colours are added to the enum.
The traditional approach is to put a sentinel value into the enumeration, at
the end, and leave the responsibility for making sure it stays the
largest value on whoever changes it. This doesn't literally avoid the
issue, but it works fairly consistently in practice. When concerned
that it's unclear, one can always add a comment to bring it to the
next programmer's attention.

--
Lowell Gilbert, embedded/networking software engineer
http://be-well.ilk.org/~lowell/
Sep 22 '08 #14

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

Similar topics

11
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't...
0
by: Vaclav Haisman | last post by:
Motivation: I have been working on some project recently that uses lots of enums with disjunctive intervals of values because it is rather convenient way to define series of constants with...
6
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
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
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
8
by: tony | last post by:
Hello! I have below a for loop and a switch in the for loop. I have also a enum called colBlowStep with some values. I have also an array called m_columnBlowStep with some strings. All items in...
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: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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
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...

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.