Enum size

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How to calculate number of the enumerated elements or in other words what is an analog of sizeof for this matter?
Below you can see an example. I have failed to find out the right way of doing it, may be you can help?
enum puzzle{ ten = 10, two = 2, three, zero = 0 };
Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

logarithm of base 2 of the largest number you use.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

wrong.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

enumerated types are integers. sizeof() will usually return 2, IIRC.

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess you really wanted to get the number of elements in the enum and
not the size of the enum itself.

Unfortunately there is no such operator, so you have to stick with the plain
old defines.

A common way to do it when you don't assign values to your elements is:

enum {
e_0,
e_1,
...
e_n,
e_max
} myEnum;

Now 'e_max' holds the number of elements.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"I guess you really wanted to get the number of elements in the enum and
not the size of the enum itself."
That is true.
I have put an example intentionaly to show that it isn't a simple order, but complex one.