Hi!
Dr E. Buxbaum wrote:
Is it possible to specify the value of an enumerated type? I was trying to port a C header file which did something like this:
typedef enum hello_everybody { hello_error = -1, hello_first = 0, hello_second, hello_third
Can this be done with GPC?
I am noot quite sure what you whant to do here. Under Pascal you can write:
type Hello_Everybody = (Hello_error, Hello_first, Hello_second, Hello_third);
and then use the ORD function to get their numerical value. ORD(Hello_Error) would be 0, ORD(Hello_First) 1 and so on. This would be an enumerated type.
I think that the use in the C-code fragment is ment to be something like this litle piece of Ada-code:
type hello_everybody is (hello_error, hello_first, hello_second, hello_third);
-- this simply creates the enumerated type,
for hello_everybody use (hello_error => -1, hello_first => 0, hello_second => 4, hello_third => 17);
-- this tells the compiler what numeric values that are going to be used -- for the enumerated symbols of that type.
(* in pascal this would mean*)
var x : hello_everybody;
begin x := hello_third; writeln(' Ord(x)=', ord(x), ' equals 17'); end.
AFIK there is no way of specifying this in standard pascal. I GPC it would be nice if it was implemented, perhaps something similar to that piece of Ada above.
Note that ord(pred(hello_second)) is 0.
The other option would be a record (similar to a C struct):
type Hello_Everybody = record Hello_Error, Hello_First, Hello_Second, Hello_Third : integer; end;
In this case you could assign any integer value you like to any of the records field, while in the case of enumeration types the ORD-value is given by the order of definition.
I don't think this was the intended function.
/Jakob :)
mailto:Jakob.Heinemann@ericsson.com Ericsson Saab Avionics AB Linköping +46 13 284249