Enum problem...

R

Rohit

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
 
C

Chris Dollin

Rohit said:
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
};
 
C

CBFalconer

Rohit said:
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;
 
R

Rohit

Rohit said:
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.
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
 
C

Chris Dollin

Rohit said:
Rohit said:
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.
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.
 
A

aburry

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
 
A

August Karlstrom

Rohit said:
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
 
J

James Kuyper

Rohit said:
Rohit said:
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.
 
G

gw7rib

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?
 
B

Bartc

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.
 
I

Ian Collins

Bartc said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top