473,799 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing enum with strings

Hi,
gcc-3.4 complains about non-integers in:

enum {IDENTIFIER = "<identifie r>", WIDGETDEF = "widgetdef" };

even if i cast the strings to integers.
Nov 15 '05 #1
10 10517
Russell Shaw <rjshawN_o@s_pa m.netspace.net. au> wrote:
gcc-3.4 complains about non-integers in:

enum {IDENTIFIER = "<identifie r>", 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
Nov 15 '05 #2
Russell Shaw wrote:
Hi,
gcc-3.4 complains about non-integers in:

enum {IDENTIFIER = "<identifie r>", 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.
Nov 15 '05 #3
Richard Bos wrote:
Russell Shaw <rjshawN_o@s_pa m.netspace.net. au> wrote:
gcc-3.4 complains about non-integers in:

enum {IDENTIFIER = "<identifie r>", 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.
Nov 15 '05 #4
Russell Shaw <rjshawN_o@s_pa m.netspace.net. au> wrote:
Hi,
gcc-3.4 complains about non-integers in:

enum {IDENTIFIER = "<identifie r>", 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[] = {
"<identifie r>",
"widgetdef" ,
};

and in C99, assuming nothing but the usual constraints

const char *enummap[] = {
.[WIDGETDEF] = "widgetdef" ,
.[IDENTIFIER] = "<identifie r>",
};

(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
Nov 15 '05 #5
Me
> >>gcc-3.4 complains about non-integers in:

enum {IDENTIFIER = "<identifie r>", 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 = "<identifie r>";

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, "<identifie r>") \
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

Nov 15 '05 #6
On Tue, 05 Jul 2005 19:17:01 -0700, Me wrote:
>>gcc-3.4 complains about non-integers in:
>>
>>enum {IDENTIFIER = "<identifie r>", 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 = "<identifie r>";


However you're creating an unnecessary pointer object here. Better is:

static const char IDENTIFIER[] = "<identifie r>";

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
Nov 15 '05 #7
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, "<identifie r>") \
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.ne t
Nov 15 '05 #8
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, "<identifie r>") \
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.ne t


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.
Nov 15 '05 #9
Russell Shaw <rjshawN_o@s_pa m.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_Keit h) 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.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4791
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 structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a lot of case I see no way how to do it.
21
4607
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 {
10
7141
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 how does one override the function to have some values return a different string? For example, suppose I have an enum with value for the types of DVD media like public enum DVD_Media {
5
1471
by: A.M-SG | last post by:
Hi, Can I have enum contain strings? Thank you, Alan
4
3171
by: guoqi zheng | last post by:
Trying to declare a enum datatype. Below works Public Enum EnumCharSet IBM037 IBM437 IBM500 End Enum
0
1309
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 import Enum >>> Weekdays = Enum('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
6
3005
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,
35
15290
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 {red, blue, green, yellow, black, purple, pink}; What I am doing now is reading from a text file a string (char *). I
3
11416
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 every string in the array and return the enum when it matches. Whats the efficient way to do this? Thanks rsennat
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9078
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.