enum safety



Hi,

Page 39 K&R2 says

'Although variables of enum types may be declared, compilers need not
check that what you store in such a variable is a valid value for the
enumeration'

gcc produces an error for the code below as I'm trying to assign a
pointer to char to the a variable of type colour. Is gcc going beyond
the call of duty? Why would the standard allow compilers to accept
such code?

#include <stdio.h>
#include <limits.h>

char* c=0;

enum colours {
Red,Blue

};


int main ()
{
enum colours purple =c ;

return 0;
}
.



Relevant Pages

  • Re: enum safety
    ... 'Although variables of enum types may be declared, compilers need not ... check that what you store in such a variable is a valid value for the ...
    (comp.lang.c)
  • Re: enum safety
    ... 'Although variables of enum types may be declared, compilers need not ... check that what you store in such a variable is a valid value for the ...
    (comp.lang.c)
  • Re: enum safety
    ... 'Although variables of enum types may be declared, compilers need not ... In this case, c is of type char*, purple is of type enum ... creates a series of constants (which, oddly enough, are of type int, ...
    (comp.lang.c)
  • Re: resolving a warning error
    ... >This works on several systems and compilers, but fails on aix xlc. ... Not if you invoke them in conforming mode: ... >warning messages and without declaring the enum? ...
    (comp.lang.c)
  • Re: Header files with enums instead of defines?
    ... You also can't #ifdef an enum. ... in those compilers you would have ... to define 'xerror' as 'enum ERRVALS xerror', ...
    (freebsd-arch)