By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

Init structure of constants

P: n/a
Hi Group,

I am trying to define a strucutre of constants. Then switch on this
structure of constant values.

In the following example of my problem the code fails to compile due
to:

case (constants.one):
In this statement, "constants.one" is not constant, but occurs in a
context that requires a constant expressio
n.

How should I define a structure of const to switch on.

Thanks

Stuart
#include <stdio>
#include <stdlib>

struct Constant
{
const int one;
const int two;
} constants ={ 65, 66 } ;

int main (int argc, char **argv)
{
char buffer = 'A';
switch (buffer)
{
case (constants.one):
break;
case (constants.two):
break;
}
return 0;
}

Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
st**********@yahoo.com.au wrote:
Hi Group,

I am trying to define a strucutre of constants. Then switch on this
structure of constant values.

In the following example of my problem the code fails to compile due
to:

case (constants.one):
In this statement, "constants.one" is not constant, but occurs in a
context that requires a constant expressio
n.

How should I define a structure of const to switch on.

<snip>

Not at all.
For case labels, you need compile time constants. In C, you
cannot obtain compile time constants via "const".
Therefore, const structures or structures with const members
will not work.

If you want to have "named compile time constants", use either
macros or enumeration constants:

#define CONST_ONE 65
#define CONST_TWO 66

enum myConstantsWithPurpose {
eConstWP_One = 42, eConstWP_Two /* automatically eConstWP_One + 1 */
};

.....
switch (bla) {
case CONST_ONE:
....
case eConstWP_Two:
....
}
Cheers
Michael

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #2

P: n/a
On 2005-11-09, Michael Mair <Mi**********@invalid.invalid> wrote:
st**********@yahoo.com.au wrote:
Hi Group,

I am trying to define a strucutre of constants. Then switch on this
structure of constant values.

In the following example of my problem the code fails to compile due
to:

case (constants.one):
In this statement, "constants.one" is not constant, but occurs in a
context that requires a constant expressio
n.

How should I define a structure of const to switch on.

<snip>

Not at all.
For case labels, you need compile time constants. In C, you
cannot obtain compile time constants via "const".
Therefore, const structures or structures with const members
will not work.

If you want to have "named compile time constants", use either
macros or enumeration constants:

#define CONST_ONE 65
#define CONST_TWO 66

enum myConstantsWithPurpose {
eConstWP_One = 42, eConstWP_Two /* automatically eConstWP_One + 1 */
};


One thing i've never understood about enums:

enum mytype {
value1 = 42, value2, value3, value4=41
};

As far as i know, that's permitted by the standard, but I can't figure
out what value2 and value3 are supposed to be in that case, or if they
are guaranteed any particular values
Nov 15 '05 #3

P: n/a
Jordan Abel <jm****@purdue.edu> writes:
[...]
One thing i've never understood about enums:

enum mytype {
value1 = 42, value2, value3, value4=41
};

As far as i know, that's permitted by the standard, but I can't figure
out what value2 and value3 are supposed to be in that case, or if they
are guaranteed any particular values


C99 6.7.2.2p3:

The identifiers in an enumerator list are declared as constants
that have type int and may appear wherever such are permitted. An
enumerator with = defines its enumeration constant as the value of
the constant expression. If the first enumerator has no =, the
value of its enumeration constant is 0. Each subsequent enumerator
with no = defines its enumeration constant as the value of the
constant expression obtained by adding 1 to the value of the
previous enumeration constant. (The use of enumerators with = may
produce enumeration constants with values that duplicate other
values in the same enumeration.) The enumerators of an enumeration
are also known as its members.

An enumerator with no specified value always has a value one greater
than the previous enumerator in the declaration, or 0 if it's the
first. In the above, you'd have
value1==42
value2==43
value3==44
value4==41

There's no requirement for the values to be ordered, or even distinct:
enum {
zero=0, zilch=0, nada=0,
one, two, three, many
};

--
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.
Nov 15 '05 #4

P: n/a
Jordan Abel wrote:
On 2005-11-09, Michael Mair <Mi**********@invalid.invalid> wrote:
st**********@yahoo.com.au wrote:
Hi Group,

I am trying to define a strucutre of constants. Then switch on this
structure of constant values.

In the following example of my problem the code fails to compile due
to:

case (constants.one):
In this statement, "constants.one" is not constant, but occurs in a
context that requires a constant expressio
n.

How should I define a structure of const to switch on.


<snip>

Not at all.
For case labels, you need compile time constants. In C, you
cannot obtain compile time constants via "const".
Therefore, const structures or structures with const members
will not work.

If you want to have "named compile time constants", use either
macros or enumeration constants:

#define CONST_ONE 65
#define CONST_TWO 66

enum myConstantsWithPurpose {
eConstWP_One = 42, eConstWP_Two /* automatically eConstWP_One + 1 */
};

One thing i've never understood about enums:

enum mytype {
value1 = 42, value2, value3, value4=41
};

As far as i know, that's permitted by the standard, but I can't figure
out what value2 and value3 are supposed to be in that case, or if they
are guaranteed any particular values


If value1 were not assigned a value: value1 == 0
As value2 is not assigned another value: value2 == value1 + 1
As value3 is not assigned another value: value3 == value2 + 1
As value4 is assigned a value, it has this very value.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #5

P: n/a
On 2005-11-09, Keith Thompson <ks***@mib.org> wrote:
Jordan Abel <jm****@purdue.edu> writes:
[...]
One thing i've never understood about enums:

enum mytype {
value1 = 42, value2, value3, value4=41
};

As far as i know, that's permitted by the standard, but I can't figure
out what value2 and value3 are supposed to be in that case, or if they
are guaranteed any particular values


C99 6.7.2.2p3:

The identifiers in an enumerator list are declared as constants
that have type int and may appear wherever such are permitted. An
enumerator with = defines its enumeration constant as the value of
the constant expression. If the first enumerator has no =, the
value of its enumeration constant is 0. Each subsequent enumerator
with no = defines its enumeration constant as the value of the
constant expression obtained by adding 1 to the value of the
previous enumeration constant. (The use of enumerators with = may
produce enumeration constants with values that duplicate other
values in the same enumeration.) The enumerators of an enumeration
are also known as its members.

An enumerator with no specified value always has a value one greater
than the previous enumerator in the declaration, or 0 if it's the
first. In the above, you'd have
value1==42
value2==43
value3==44
value4==41


gah - i screwed up my own example - i meant:
suppose value1 is 41, and value4 is 42? what are value2 and value3?

but you've answered that question.

I was sure they were required to be distinct if not actually specified
as identical, but i guess i was wrong
Nov 15 '05 #6

P: n/a

"Jordan Abel" <jm****@purdue.edu> wrote in message
news:sl*******************@random.yi.org...
On 2005-11-09, Michael Mair <Mi**********@invalid.invalid> wrote:
st**********@yahoo.com.au wrote:
Hi Group,

I am trying to define a strucutre of constants. Then switch on this
structure of constant values.

In the following example of my problem the code fails to compile due
to:

case (constants.one):
In this statement, "constants.one" is not constant, but occurs in a
context that requires a constant expressio
n.

How should I define a structure of const to switch on. <snip>

Not at all.
For case labels, you need compile time constants. In C, you
cannot obtain compile time constants via "const".
Therefore, const structures or structures with const members
will not work.

If you want to have "named compile time constants", use either
macros or enumeration constants:

#define CONST_ONE 65
#define CONST_TWO 66

enum myConstantsWithPurpose {
eConstWP_One = 42, eConstWP_Two /* automatically eConstWP_One + 1 */
};


One thing i've never understood about enums:

enum mytype {
value1 = 42, value2, value3, value4=41
};

As far as i know, that's permitted by the standard, but I can't figure
out what value2 and value3 are supposed to be in that case


43 and 44 respectively.

, or if they are guaranteed any particular values


They are. Reread 6.7.2.2
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.