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;
} 6 4573 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.
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
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.
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.
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alberto Vera |
last post by:
Hello:
I have the next structure:
How Can I make it using Python?
How Can I update the value of 6?
|
by: jsnX |
last post by:
Say I would like my namespace to have some constants associated with
it, like this:
==========
=====
namespace broadpen
{
struct defaults
{
static const double w = 10.0;
static const double...
|
by: Brian K. Michalk |
last post by:
I know I can assign default values to a structure during
initialization, but I have a routine that accumulates a lot of floats
into an array, and then I would like to assign them to a (reference to...
|
by: Paul |
last post by:
Hello,
Consider this case:
Init(params) does some initialization and there is a function
Connect(params). The system starts and is idle until some one calls
Connect, as soon as first...
|
by: Stef |
last post by:
Hello,
I have a question:
Is it possible to init a 2d array of structures to zero ?
For example with array I do.
int array = {0}
but:
|
by: Clemens Chiba - Greentube I.E.S. AG |
last post by:
Hi!
If the user has no LAN connection I have to dialup to the internet
in my application. This works fine, but in some case I'll have to
change the phone number (if the provider changes) and the...
|
by: Wylbur via DotNetMonster.com |
last post by:
Hello to all of you geniuses,
I'm having a problem trying to get an Init handler to fire for a Placeholder
control
at the initialization phase. I’ve posted this problem to 3 other ASP.NET...
|
by: Polska, Ursula |
last post by:
Hello experts, could explain to initialize a structure?
Private Structure sEG
Public EN As Integer = 1
Public EG1 As Integer = 2
Public EG9 As String = "EG, 14.2"
End Structure
Please no...
|
by: Bob Day |
last post by:
Using VS 2003, VB.NET, MSDE
Is there a way to itterate through the items in a structure? Consider the
structure below, which holds the name of wave files. I would like to
itterate through them...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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?
| |