tags:

views:

188

answers:

5

Hi, I want to define a type named Int_1_100_Type which is an integer variable in the range from 1 to 100. How should i typedef this one? for eg: i am passing this variable to a function which accepts variable of type Int_1_100_Type. i.e funca(Int_1_100_Type Var1)

Thanks Maddy

+4  A: 

You can't, C has no such functionality. You can of course typedef an int:

typedef int int_1_100_Type;

but there is no way of restricting its range. In C++, you could create a new type with this functionality, but I think very few people would bother - you just need to put range checks in the function(s) that use the type.

anon
Perhaps one could fake it with an `enum`, if one was clinically insane? Of course, even that wouldn't prevent anyone from passing in any old integer.
tzaman
Not really. An enum always has a range that includes 0, and the number of possible values is always a power of 2. Classical example: the range of `enum rights {Read = 1, Write = 2};` includes 0 (no rights) and 3 (both).
MSalters
You could even `typedef` a `char` in this case.
caf
@MSalters: not true. That is a common use case of an enumeration, yes, but the bitwise combinations of enumerations are not technically a part of the enumeration. The whole thing is rather worthless, though, since you can store any value into an `enum` typed variable.
Matt B.
@Matt B: that's why I said they were part of **the range**. And you're wrong wrt to "any value". The standard certainly doesn't guarantee that you can cast INT_MAX to `enum rights` from my previous example, e.g. when `sizeof(rights) < sizeof(int)`
MSalters
A: 

You can't put that kind of limit on the range of an integer.

You can of course typedef it anyway:

typedef int int_1_100;

or even better:

typedef unsigned int int_1_100;

But nothing in C will prevent you from writing:

int_1_100 x = 1000;

To implement something like that, you need to hide the implementation, but that will make it harder to initialize the value (and impossible to allocate values of the type on the stack, with the hiding intact).

unwind
A: 

In C++, there would be a way to do this by writing a class that would act like an integer, but it would be way too much effort and way too heavyweight a solution to be practical.

Thanks Naveen for pointing out the question was C only.

wich
question is tagged as c not c++.
Naveen
Uh, yes my bad... This would indeed only be possible in C++
wich
A: 

There's no way in c to define a type that must be in a specific range. You could however check each value in your functions, e.g.

int funca(int Var1)
{
    assert(Var1 >= 1);
    assert(Var1 < 101);
    ...
}
Scott Wales
+4  A: 

Of course you can. All you need is a little object-based C.

create a file with a struct and some members

typedef struct s_foo {
  int member;
} Foo;

Foo* newFoo(int input);   // ctor
void get(Foo *f);         // accessor

Enforce your condition in the mutator/ctor

If you do this in its own file, you can hide the impl of the class as well, you can do oo-like C

shiva