Check if an int value is within valid range of enum?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Then I read an int value from file or stdin. How do I verify that it is one of the valid values of that enum? Of course I don't mean to do something like
Code:
if (value != VAL_A && value != VAL_B && value != VAL_C ....)
Well, first, how would your input stream creator know what values had been used in your enumeration type? In other words, why would you be using an enumeration type to parse an input stream?
The obvious solution would be to set up an array, initialize it to your enumeration values, sort it, and do a binary search to find a match, if any. Note, however, that there is no restriction on the values assigned to enumeration types, so, for example, this is a perfectly valid enumeration:
I'm using enum for a config option. If this option has some weird value, the program would not function properly. I use switch sentences for handling these options, so the program eventually finds out the value is wrong, but this could happen only when the switch block gets executed, which could be after a long time. I'd like to warn user of this shortly after the program is started.
I'm writing in C, not C++.
The user knows which numbers are valid, but there is always a possibility of a human mistake or config file corruption ...
Quote:
The obvious solution would be to set up an array, initialize it to your enumeration values, sort it, and do a binary search to find a match
Could you please explain this in more details, perhaps some example code? I don't know how to:
1) Determine what length should that array have. I don't know how to get the enum length, sizeof(anyEnum) returns 4.
2) Initialize that array to enum values. Without explicitly naming every value of course.
It is however, not exactly what I meant. I need enum, because after I'd read and check the value, I'd like to operate only with labels, not numbers and not arrays, because labels tell you much more.
I'd like to declare all values only in enum. Then I would use for example something like you suggested, but all the values and length of array should be determined automatically so I will have to change only the enum definition when I decide to change/add/remove an option. Is this possible in C?
$ cat test5.c
#include <stdio.h>
static struct myEnum {
int A;
int B;
int C;
int D;
int E;
int F;
} Val = {0, 2, 10, 12, 25, 30};
int main(int argc, char ** argv)
{
int test;
printf("sizeof(Val)=%d\nsizeof(Val.A)=%d\n", sizeof(Val), sizeof(Val.A));
for (test = 0; test < sizeof(Val)/sizeof(Val.A); ++test) {
printf("test %d Val = %d\n",test, *(&Val.A+test));
}
}
$ ./test5
sizeof(Val)=24
sizeof(Val.A)=4
test 0 Val = 0
test 1 Val = 2
test 2 Val = 10
test 3 Val = 12
test 4 Val = 25
test 5 Val = 30
And your references would look almost the same: Val.A instead of Val_A.
It gets a little trickier if not all your struct elements are the same type, but remains, I think, workable.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.