Iterate an enum

S

Steven Woody

is there any way in C leting me iterate all integer constants in a enum
type ? their values might not be continue.

thanks.

-
woody
 
Z

Zara

is there any way in C leting me iterate all integer constants in a enum
type ? their values might not be continue.

thanks.

-
woody


enum {FIRST=0,SECOND=554,THIRD=1000};

Decorate it with your typedef's etc.. as needed.

You just can't repeat a value inside the enum.

Regards,

Zara
 
K

Keith Thompson

Zara said:
enum {FIRST=0,SECOND=554,THIRD=1000};

Decorate it with your typedef's etc.. as needed.

I don't think that's what he was looking for. I think that, given
your declaration, he wants a loop that iterates over the values FIRST,
SECOND, and THIRD. There's no direct way to do that in C.

You can create an array whose values are the enumeration values, but
you have to either maintain the array in parallel with the enumeration
type or generate them as C source from some external source.
You just can't repeat a value inside the enum.

Yes, you can:

enum { zero=0, zilch=0, nada=0, uno=1, nothing=0 };
 
S

Steven Woody

Keith said:
I don't think that's what he was looking for. I think that, given
your declaration, he wants a loop that iterates over the values FIRST,
SECOND, and THIRD. There's no direct way to do that in C.

yes, this is what i want. it seems ( from you reply ) there is no
solution in C. it's however, easy in many other advanced language.
 
I

Ico

Steven Woody said:
yes, this is what i want. it seems ( from you reply ) there is no
solution in C. it's however, easy in many other advanced language.

That is what advanced languages are for. C is no such language.

Besides, what you describing as a functionality of 'advanced languages'
is usually something different then an enum. Those are often called
'lists', 'sets', 'arrays', 'tables', 'associative arrays', 'tuples' et
cetera.

You might be better off using one of those languages, or you could
create your own implementation of a data structure which provides the
functionality you need.

Ico
 
K

Kenny McCormack

Steven Woody said:
yes, this is what i want. it seems ( from you reply ) there is no
solution in C. it's however, easy in many other advanced language.

ITYM:

.... easy in an advanced language.
 
P

Peter Nilsson

Steven said:
yes, this is what i want. it seems ( from you reply ) there is no
solution in C.

The C solution is given in the very next paragraph of Keith's reply
(below.)
it's however, easy in many other advanced language.

Well then... if those languages are available to your project, use
them.

Being a C programmer doesn't limit you to C. Any programmer worth their
salt is going to use the best tool for the job. That tool isn't always
C.

Both the parallel maintenance and source code generation methods have
been
discussed before in comp.lang.c. Neither is especially elegant, but
neither is
particularly difficult either.

That said, you seem to be in the camp...

I have a problem A, I think the solution is B.
I don't know how to do B, so I'll ask about B.
You should really be asking about A, showing B as your attempt.

That way, not only might you receive comments on B, you might
actually get better alternatives on how to solve your real problem A.
 

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

Similar Threads


Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,478
Latest member
ReginaldVi

Latest Threads

Top