Hi,
gcc-3.4 complains about non-integers in:
enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"};
even if i cast the strings to integers. 10 10465
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote: gcc-3.4 complains about non-integers in:
enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"};
And so it should. Enumeration constants can only have integral values.
If you want enum constants with string values, you're probably trying to
do something which you needn't, or shouldn't, do. Why would you need
such constants in the first place?
even if i cast the strings to integers.
That doesn't even make sense. What would you cast to integer, a pointer
to a string literal? That's possible, in theory - but it doesn't make
sense. The value will be useless.
Richard
Russell Shaw wrote: Hi, gcc-3.4 complains about non-integers in:
enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"};
even if i cast the strings to integers.
Well, what did you expect?
The enum identifiers have integer values. Casting a string
to an integer doesn't get you anything terribly interesting
or reliable.
--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
Richard Bos wrote: Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
gcc-3.4 complains about non-integers in:
enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"};
And so it should. Enumeration constants can only have integral values. If you want enum constants with string values, you're probably trying to do something which you needn't, or shouldn't, do. Why would you need such constants in the first place?
Because i'm using the constants in a lexer, and they can do double duty
for error message printouts. I need to use them multiple places in the
code, and if i use the strings directly, i'm not guaranteed to get the
same pointer value every time. even if i cast the strings to integers.
That doesn't even make sense. What would you cast to integer, a pointer to a string literal? That's possible, in theory - but it doesn't make sense. The value will be useless.
Richard
char *str = "abcd"; declares a pointer to a string, so i wanted to
use pointers like this as inits for the enum.
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote: Hi, gcc-3.4 complains about non-integers in:
enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"};
even if i cast the strings to integers.
Enum's can't be pointers, only integers. The typical solution is to map
the enum string description to the enum itself using an array:
In C89, assuming your enums are sequential and start from zero
const char *enummap[] = {
"<identifier>",
"widgetdef",
};
and in C99, assuming nothing but the usual constraints
const char *enummap[] = {
.[WIDGETDEF] = "widgetdef",
.[IDENTIFIER] = "<identifier>",
};
(Note that the array will be as large as your largest enum, so this might
not work out well if your enums are meant to be utilized bitwise.)
- Bill
> >>gcc-3.4 complains about non-integers in: enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"};
And so it should. Enumeration constants can only have integral values. If you want enum constants with string values, you're probably trying to do something which you needn't, or shouldn't, do. Why would you need such constants in the first place?
Because i'm using the constants in a lexer, and they can do double duty for error message printouts. I need to use them multiple places in the code, and if i use the strings directly, i'm not guaranteed to get the same pointer value every time.
const char * const IDENTIFIER = "<identifier>";
Now you are guaranteed (and you really don't want to use the string
constant directly anyway, your compiler can't warn against inadvertant
typos that way). Even if your implementation allows something that
questionable, it doesn't guarantee they can be casted back to a string
or even that are distinct values when casted to an integer type. For
what you're trying to do, I would probably do something like:
#define TOKEN(F) \
F(IDENTIFIER, "<identifier>") \
F(WIDGETDEF, "widgetdef")
#define tok(a,b) a = b,
enum Token {
TOKEN(tok)
NUM_TOKENS
};
#undef tok
#define tok(a,b) b,
const char * const token2str[NUM_TOKENS] = {
TOKEN(tok)
};
#undef tok
On Tue, 05 Jul 2005 19:17:01 -0700, Me wrote: >>gcc-3.4 complains about non-integers in: >> >>enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"}; > > And so it should. Enumeration constants can only have integral values. > If you want enum constants with string values, you're probably trying to > do something which you needn't, or shouldn't, do. Why would you need > such constants in the first place?
Because i'm using the constants in a lexer, and they can do double duty for error message printouts. I need to use them multiple places in the code, and if i use the strings directly, i'm not guaranteed to get the same pointer value every time.
const char * const IDENTIFIER = "<identifier>";
However you're creating an unnecessary pointer object here. Better is:
static const char IDENTIFIER[] = "<identifier>";
A string literal is essentially an unnamed static array of char, all you
need to do is create a named version, as this does.
Lawrence
On 5 Jul 2005 19:17:01 -0700, "Me" <an*****************@yahoo.com>
wrote: <snip> For what you're trying to do, I would probably do something like:
#define TOKEN(F) \ F(IDENTIFIER, "<identifier>") \ F(WIDGETDEF, "widgetdef")
#define tok(a,b) a = b, enum Token { TOKEN(tok) NUM_TOKENS }; #undef tok
#define tok(a,b) b, const char * const token2str[NUM_TOKENS] = { TOKEN(tok) }; #undef tok
Obviously you meant #define tok(a,b) a, in the first case
and #define tok(a,b) [a] = b, or just b, in the second.
- David.Thompson1 at worldnet.att.net
Dave Thompson wrote: On 5 Jul 2005 19:17:01 -0700, "Me" <an*****************@yahoo.com> wrote:
<snip> For what you're trying to do, I would probably do something like:
#define TOKEN(F) \ F(IDENTIFIER, "<identifier>") \ F(WIDGETDEF, "widgetdef")
#define tok(a,b) a = b, enum Token { TOKEN(tok) NUM_TOKENS }; #undef tok
#define tok(a,b) b, const char * const token2str[NUM_TOKENS] = { TOKEN(tok) }; #undef tok
Obviously you meant #define tok(a,b) a, in the first case and #define tok(a,b) [a] = b, or just b, in the second.
- David.Thompson1 at worldnet.att.net
You should be able to cast a const string pointer to an int so that
one could do: enum {an_enum = (int)"the first enum"}. It's cleaner
than #defines.
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
[...] You should be able to cast a const string pointer to an int so that one could do: enum {an_enum = (int)"the first enum"}. It's cleaner than #defines.
gcc complains "enumerator value for `an_enum' not integer constant".
The message is actually slightly misleading; it has to be a constant
expression, not an integer constant. But gcc is correct; a cast of an
address cannot be part of an integer constant expression.
You can convert a pointer to an int in a context that doesn't require
a constant expression, but the result is not necessarily useful. For
example, if pointers are 8 bytes and ints are 4 bytes, two distinct
pointer values might convert to the same int value.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <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 wrote: Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes: [...] You should be able to cast a const string pointer to an int so that one could do: enum {an_enum = (int)"the first enum"}.
The type of "the first enum" is not const qualified.
It's cleaner than #defines. gcc complains "enumerator value for `an_enum' not integer constant".
The message is actually slightly misleading; it has to be a constant expression, not an integer constant.
More precisely, it must be an integer constant expression.
But gcc is correct; a cast of an address cannot be part of an integer constant expression.
You can convert a pointer to an int in a context that doesn't require a constant expression, but the result is not necessarily useful. For example, if pointers are 8 bytes and ints are 4 bytes, two distinct pointer values might convert to the same int value.
This notwithstanding, and as has been pointed out elsethread, there's
always...
const char the_first_enum[] = "the first enum";
Synchronising enums and strings has also been discussed in clc before.
--
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bart Goeman |
last post by:
Hi,
I have a question about how to put redundant information in data
structures, initialized at compile time. This is often necessary
for performance reasons and can't be done at run time (data...
|
by: Andreas Huber |
last post by:
Hi there
Spending half an hour searching through the archive I haven't found a
rationale for the following behavior.
using System;
// note the missing Flags attribute
enum Color
{
|
by: Ken Allen |
last post by:
The ToString() function, when applied to a variable that is an enumeration
type, results in a string that is the name of the enumerated value that was
defined in the source code. This is cool, but...
|
by: A.M-SG |
last post by:
Hi,
Can I have enum contain strings?
Thank you,
Alan
|
by: guoqi zheng |
last post by:
Trying to declare a enum datatype.
Below works
Public Enum EnumCharSet
IBM037
IBM437
IBM500
End Enum
|
by: Ben Finney |
last post by:
Howdy all,
I've uploaded enum 0.3 to the Cheeseshop.
<URL:http://cheeseshop.python.org/pypi/enum/>
Enumerations are now sequences, iterable (as before) *and* indexable::
>>> from enum...
|
by: giannik |
last post by:
I have an Enum Structure
Public Enum MyEnum
EnumVal1=0
EnumVal2=1
EnumVal2=2
end enum
I save in an access database this enum value as an integer (0=EnumVal1,
|
by: dtschoepe |
last post by:
Greetings.
I am working on an assignment and can't seem to get the right concept
for something I'm attempting to do with enum data types.
I have defined the following in my code:
enum color...
|
by: rsennat |
last post by:
Hi,
Converting enum to strings is simple. Just we can use the enum as the i ndex to get the string from an array of strings.
But how do I convert string to enum. Should I need to compare with...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |