enum question

Discussion in 'C Programming' started by James Brown, Oct 22, 2005.

  1. James Brown

    James Brown Guest

    I have the following enum declared:

    enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

    (it goes on and on like that)

    This is what I would like to do:

    TOKEN t1 = TOK_ID; // ok
    TOKEN t2 = 5; // compile error (cannot convert from
    const int to 'enum TOKEN')
    TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

    could someone clarify if the 3rd example is ok or not, and what type
    of problem I might expect if it isn't ok?

    What I am trying to do is represent ASCII values 0-127 as TOKENs (this is
    why I
    started the TOKEN enum off at '1000' so I had plenty of space at the
    start....and I don't
    really want to type out 127 values into my enum declaration....can anybody
    suggest
    an alternate solution?

    thanks,
    James
    James Brown, Oct 22, 2005
    #1
    1. Advertising

  2. James Brown

    Chad Guest

    James Brown wrote:
    > I have the following enum declared:
    >
    > enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >
    > (it goes on and on like that)
    >
    > This is what I would like to do:
    >
    > TOKEN t1 = TOK_ID; // ok
    > TOKEN t2 = 5; // compile error (cannot convert from
    > const int to 'enum TOKEN')
    > TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???
    >
    > could someone clarify if the 3rd example is ok or not, and what type
    > of problem I might expect if it isn't ok?
    >
    > What I am trying to do is represent ASCII values 0-127 as TOKENs (this is
    > why I
    > started the TOKEN enum off at '1000' so I had plenty of space at the
    > start....and I don't
    > really want to type out 127 values into my enum declaration....can anybody
    > suggest
    > an alternate solution?
    >
    > thanks,
    > James


    In the expression:

    enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

    TOK_ID, TOK_NUMBER, and TOK_STRING are constant integer values. In this
    case, TOK_ID is = 1000, then TOK_NUMBER is 1001. The reason why the
    compiler is b-tching is because you are trying to modify the constant
    integer TOK_NUMBER (which has a value of 1001).
    Chad, Oct 22, 2005
    #2
    1. Advertising

  3. "James Brown" <dont_bother> writes:
    > I have the following enum declared:
    >
    > enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >
    > (it goes on and on like that)
    >
    > This is what I would like to do:
    >
    > TOKEN t1 = TOK_ID; // ok
    > TOKEN t2 = 5; // compile error (cannot convert from
    > const int to 'enum TOKEN')
    > TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???
    >
    > could someone clarify if the 3rd example is ok or not, and what type
    > of problem I might expect if it isn't ok?


    The declaration "enum TOKEN { ... };" creates a type called
    "enum TOKEN". It does not create a type called TOKEN.

    Given the type declaration, the declaration
    enum TOKEN t2 = 5;
    is perfectly legal.

    I suspect you're using a C++ compiler. C++ is a different language
    with different rules; comp.lang.c++ is down the hall on the left.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 22, 2005
    #3
  4. "Chad" <> writes:
    > James Brown wrote:
    >> I have the following enum declared:
    >>
    >> enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >>
    >> (it goes on and on like that)
    >>
    >> This is what I would like to do:
    >>
    >> TOKEN t1 = TOK_ID; // ok
    >> TOKEN t2 = 5; // compile error (cannot convert from
    >> const int to 'enum TOKEN')
    >> TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???
    >>
    >> could someone clarify if the 3rd example is ok or not, and what type
    >> of problem I might expect if it isn't ok?
    >>
    >> What I am trying to do is represent ASCII values 0-127 as TOKENs
    >> (this is why I started the TOKEN enum off at '1000' so I had plenty
    >> of space at the start....and I don't really want to type out 127
    >> values into my enum declaration....can anybody suggest an alternate
    >> solution?

    >
    > In the expression:
    >
    > enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };


    That's a declaration, not an expression.

    > TOK_ID, TOK_NUMBER, and TOK_STRING are constant integer values. In this
    > case, TOK_ID is = 1000, then TOK_NUMBER is 1001.


    Yes.

    > The reason why the
    > compiler is b-tching is because you are trying to modify the constant
    > integer TOK_NUMBER (which has a value of 1001).


    Look again. There's nothing in the original poster's code that
    attempts to modify TOK_NUMBER.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 22, 2005
    #4
  5. James Brown

    Chad Guest

    Keith Thompson wrote:
    > "James Brown" <dont_bother> writes:
    > > I have the following enum declared:
    > >
    > > enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    > >
    > > (it goes on and on like that)
    > >
    > > This is what I would like to do:
    > >
    > > TOKEN t1 = TOK_ID; // ok
    > > TOKEN t2 = 5; // compile error (cannot convert from
    > > const int to 'enum TOKEN')
    > > TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???
    > >
    > > could someone clarify if the 3rd example is ok or not, and what type
    > > of problem I might expect if it isn't ok?

    >
    > The declaration "enum TOKEN { ... };" creates a type called
    > "enum TOKEN". It does not create a type called TOKEN.
    >
    > Given the type declaration, the declaration
    > enum TOKEN t2 = 5;
    > is perfectly legal.
    >
    > I suspect you're using a C++ compiler. C++ is a different language
    > with different rules; comp.lang.c++ is down the hall on the left.
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.


    I'm calling an offsides on this one. Maybe I mis-understood the
    question. I always thought when you has a construction like

    enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

    Then TOK_NUMBER = 1001 and TOK_STRING = 1002. Both of these being
    constant integers (Page 39 of the "C Programming Language" by K & R).
    Hence these values could not be modified because they are constant.
    Chad, Oct 22, 2005
    #5
  6. James Brown

    Michael Mair Guest

    Chad wrote:
    > Keith Thompson wrote:
    >
    >>"James Brown" <dont_bother> writes:
    >>
    >>>I have the following enum declared:
    >>>
    >>>enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >>>
    >>>(it goes on and on like that)
    >>>
    >>>This is what I would like to do:
    >>>
    >>>TOKEN t1 = TOK_ID; // ok
    >>>TOKEN t2 = 5; // compile error (cannot convert from
    >>>const int to 'enum TOKEN')
    >>>TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???
    >>>
    >>>could someone clarify if the 3rd example is ok or not, and what type
    >>>of problem I might expect if it isn't ok?

    >>
    >>The declaration "enum TOKEN { ... };" creates a type called
    >>"enum TOKEN". It does not create a type called TOKEN.
    >>
    >>Given the type declaration, the declaration
    >> enum TOKEN t2 = 5;
    >>is perfectly legal.
    >>
    >>I suspect you're using a C++ compiler. C++ is a different language
    >>with different rules; comp.lang.c++ is down the hall on the left.

    >
    > I'm calling an offsides on this one. Maybe I mis-understood the
    > question. I always thought when you has a construction like
    >
    > enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >
    > Then TOK_NUMBER = 1001 and TOK_STRING = 1002. Both of these being
    > constant integers (Page 39 of the "C Programming Language" by K & R).
    > Hence these values could not be modified because they are constant.


    I am not sure what you mean.
    The
    enum TOKEN {
    ....
    };
    declaration gives us the type "enum TOKEN", quite like
    struct foo {
    ....
    };
    gives us the type "struct foo".

    So,
    enum TOKEN t2;
    t2 = 5;
    or
    enum TOKEN t2 = 5;
    are structurally not different from
    struct foo bar;
    bar = baz;
    or
    struct foo bar = baz;
    where baz is of type struct foo.

    Declaring t2 certainly does not change any of the enumeration
    constants, neither does initializing t2.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Oct 22, 2005
    #6
  7. James Brown

    James Brown Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "James Brown" <dont_bother> writes:
    >> I have the following enum declared:
    >>
    >> enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >>
    >> (it goes on and on like that)
    >>
    >> This is what I would like to do:
    >>
    >> TOKEN t1 = TOK_ID; // ok
    >> TOKEN t2 = 5; // compile error (cannot convert from
    >> const int to 'enum TOKEN')
    >> TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???
    >>
    >> could someone clarify if the 3rd example is ok or not, and what type
    >> of problem I might expect if it isn't ok?

    >
    > The declaration "enum TOKEN { ... };" creates a type called
    > "enum TOKEN". It does not create a type called TOKEN.
    >
    > Given the type declaration, the declaration
    > enum TOKEN t2 = 5;
    > is perfectly legal.
    >
    > I suspect you're using a C++ compiler. C++ is a different language
    > with different rules; comp.lang.c++ is down the hall on the left.
    >
    > --
    > Keith Thompson (The_Other_Keith)
    > <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*>
    > <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.


    Hi,
    thanks for your answer - and you're right, I am using C++ but its useful for
    me
    to appreciate the differences....I'll repost on c.l.c++

    thanks,
    James
    James Brown, Oct 22, 2005
    #7
  8. "Chad" <> writes:
    > Keith Thompson wrote:
    >> "James Brown" <dont_bother> writes:
    >> > I have the following enum declared:
    >> >
    >> > enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >> >
    >> > (it goes on and on like that)
    >> >
    >> > This is what I would like to do:
    >> >
    >> > TOKEN t1 = TOK_ID; // ok
    >> > TOKEN t2 = 5; // compile error (cannot convert from
    >> > const int to 'enum TOKEN')
    >> > TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???
    >> >
    >> > could someone clarify if the 3rd example is ok or not, and what type
    >> > of problem I might expect if it isn't ok?

    >>
    >> The declaration "enum TOKEN { ... };" creates a type called
    >> "enum TOKEN". It does not create a type called TOKEN.
    >>
    >> Given the type declaration, the declaration
    >> enum TOKEN t2 = 5;
    >> is perfectly legal.
    >>
    >> I suspect you're using a C++ compiler. C++ is a different language
    >> with different rules; comp.lang.c++ is down the hall on the left.

    [...]
    > I'm calling an offsides on this one. Maybe I mis-understood the
    > question. I always thought when you has a construction like
    >
    > enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >
    > Then TOK_NUMBER = 1001 and TOK_STRING = 1002. Both of these being
    > constant integers (Page 39 of the "C Programming Language" by K & R).
    > Hence these values could not be modified because they are constant.


    Of course you can't modify TOK_NUMBER or TOK_STRING.

    The posted code (see above) doesn't attempt to do so, and I don't see
    anything that would lead you to believe that it does.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 23, 2005
    #8
  9. James Brown a écrit :
    > enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >
    > TOKEN t1 = TOK_ID; // ok
    > TOKEN t2 = 5; // compile error (cannot convert from
    > const int to 'enum TOKEN')


    Be sure you are using a C compiler. The C-language is not that strongly
    typed. This line is fine C.

    > TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???


    It's fine C too.

    --
    C is a sharp tool
    Emmanuel Delahaye, Oct 30, 2005
    #9
  10. James Brown

    Eric Sosman Guest

    Emmanuel Delahaye wrote:
    > James Brown a écrit :
    >
    >> enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >>
    >> TOKEN t1 = TOK_ID; // ok
    >> TOKEN t2 = 5; // compile error (cannot convert
    >> from const int to 'enum TOKEN')

    >
    >
    > Be sure you are using a C compiler. The C-language is not that strongly
    > typed. This line is fine C.
    >
    >> TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

    >
    >
    > It's fine C too.


    Ah. This is obviously some strange usage of the word
    "fine" that I wasn't previously aware of.

    enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    TOKEN t1 = TOK_ID;
    TOKEN t2 = 5;
    TOKEN t3 = (TOKEN)5;

    gcc -W -Wall -ansi -pedantic -c token.c
    token.c:2: error: parse error before "t1"
    token.c:2: warning: type defaults to `int' in declaration of `t1'
    token.c:2: error: ISO C forbids data definition with no type or storage
    class
    token.c:3: error: parse error before "t2"
    token.c:3: warning: type defaults to `int' in declaration of `t2'
    token.c:3: error: ISO C forbids data definition with no type or storage
    class
    token.c:4: error: parse error before "t3"
    token.c:4: warning: type defaults to `int' in declaration of `t3'
    token.c:4: error: `TOKEN' undeclared here (not in a function)
    token.c:4: error: parse error before numeric constant

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 31, 2005
    #10
  11. Eric Sosman a écrit :
    >>> enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
    >>>
    >>> TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

    >>
    >> It's fine C too.

    >
    > Ah. This is obviously some strange usage of the word
    > "fine" that I wasn't previously aware of.


    Ah sh*t, I missed the 'enum' word... Sorry about that.

    --
    C is a sharp tool
    Emmanuel Delahaye, Oct 31, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    520
  2. Eric Sosman
    Replies:
    3
    Views:
    770
    Mayeul
    Feb 26, 2010
  3. Wojtek
    Replies:
    1
    Views:
    482
  4. Lew
    Replies:
    0
    Views:
    524
  5. Roedy Green
    Replies:
    0
    Views:
    717
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page