Init structure of constants

S

stuie_norris

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;
}
 
M

Michael Mair

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
 
J

Jordan Abel

<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
 
M

Michael Mair

Jordan said:
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
 
K

Keith Thompson

Jordan Abel said:
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
};
 
J

Jordan Abel

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
 
M

Mark B

Jordan Abel said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top