472,971 Members | 2,224 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 software developers and data experts.

Array initialization error "expected an expression"

I am trying to initialize an array whose initializers depend on value
of Enums. I take enum and then decide the initializer value, so that
even if enum value changes because of addition to list even then I
should be able to get correct value for the array element. I need
value and state to be present in a single byte thats why I use macros.
Here is what my code look like:

typedef enum
{
SwitchIn_Neutral,
SwitchIn_Active

} SwitchIn_value_t;

typedef enum
{
VALUE_FULLVALID,
VALUE_UNAVAIL,
VALUE_INACCURATE,
VALUE_DISABLED,
VALUE_ERROR
}valueState_t;
#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))
#define NEUTRAL_VALID VALUE_TO_BYTE(SwitchIn_Neutral,VALUE_FULLVALID)
#define ACTIVE_VALID VALUE_TO_BYTE(SwitchIn_Active,VALUE_FULLVALID)
#define DEFAULT_ERROR VALUE_TO_BYTE(SwitchIn_Neutral,VALUE_ERROR)

char DL_SwitchIn_ConfTable[3][10] =
{

{NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_V ALID,DEFAULT_ERROR,

DEFAULT_ERROR,NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_E RROR,DEFAULT_ERROR},

{NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ ERROR,NEUTRAL_VALID,

ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR,NEUTRAL_V ALID,ACTIVE_VALID},

{ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_V ALID,ACTIVE_VALID,

NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VA LID,NEUTRAL_VALID}
};

But I am getting error saying 'expected an expression' at array
initialization.

I suppose my initializers are constant expressions, but what is the
reason for this error?
Sep 18 '08 #1
9 11040
Rohit wrote:
I am trying to initialize an array whose initializers depend on value
of Enums. I take enum and then decide the initializer value, so that
even if enum value changes because of addition to list even then I
should be able to get correct value for the array element. I need
value and state to be present in a single byte thats why I use macros.
(fx:snip)
#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))
You should note that this restricts both of your enums to values
less than 16, or rather that the values are taken mod 16 by your
macro. (So you can't just go on adding values without fixing the
macro. I'd suggest making sure your unit tests protect you against
this as far as they can.)

(fx:snip)
But I am getting error saying 'expected an expression' at array
initialization.
I suspect that your macro really is broken across two lines, as
it appears above, which means that the expansions are not legal
expressions.

My evidence for this is that if I leave the lines as they are,
gcc says:

sw.c:19: error: expected identifier or '(' before '&' token

but if I join those two lines together, the only diagnostic I get
is (paraphrased) "I CAN HAZ main?".

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Sep 18 '08 #2
On Sep 18, 2:56*pm, Chris Dollin <chris.dol...@hp.comwrote:
Rohit wrote:

(fx:snip)
#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))

You should note that this restricts both of your enums to values
less than 16, or rather that the values are taken mod 16 by your
macro. (So you can't just go on adding values without fixing the
macro. I'd suggest making sure your unit tests protect you against
this as far as they can.)
Its done intentionally since these enums have an upper limit of 10.
(fx:snip)
But I am getting error saying 'expected an expression' at array
initialization.

I suspect that your macro really is broken across two lines, as
it appears above, which means that the expansions are not legal
expressions.
Macro is not broken its continuous and on a single line. I guess it
exceeded browser window width so it appears broken.
My evidence for this is that if I leave the lines as they are,
gcc says:

* sw.c:19: error: expected identifier or '(' before '&' token
Rohit
Sep 18 '08 #3
Rohit wrote:
On Sep 18, 2:56*pm, Chris Dollin <chris.dol...@hp.comwrote:
>Rohit wrote:

(fx:snip)
#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))

You should note that this restricts both of your enums to values
less than 16, or rather that the values are taken mod 16 by your
macro. (So you can't just go on adding values without fixing the
macro. I'd suggest making sure your unit tests protect you against
this as far as they can.)

Its done intentionally since these enums have an upper limit of 10.
I suggest comments to that effect, as well as tests.
>(fx:snip)
But I am getting error saying 'expected an expression' at array
initialization.

I suspect that your macro really is broken across two lines, as
it appears above, which means that the expansions are not legal
expressions.

Macro is not broken its continuous and on a single line.
Not as it's sent, it isn't.
I guess it exceeded browser window width so it appears broken.
I'm not using a browser to read news. If I were, I'd have checked
to see if it was just a line-wrap issue. As received, that line is
broken.

Make /sure/ that it really is a single line in your source code.
Since it compiles without a syntax problem here if it's one line,
but not if it's two, you need to be double sure -- maybe even
long double sure.

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Sep 18 '08 #4
On 18 Sep, 11:10, Rohit <papakap...@gmail.comwrote:
On Sep 18, 2:56*pm, Chris Dollin <chris.dol...@hp.comwrote:
Rohit wrote:
#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))
You should note that this restricts both of your enums to values
less than 16, or rather that the values are taken mod 16 by your
macro. (So you can't just go on adding values without fixing the
macro. I'd suggest making sure your unit tests protect you against
this as far as they can.)

Its done intentionally since these enums have an upper limit of 10.
But I am getting error saying 'expected an expression' at array
initialization.
I suspect that your macro really is broken across two lines, as
it appears above, which means that the expansions are not legal
expressions.

Macro is not broken its continuous and on a single line. I guess it
exceeded browser window width so it appears broken.
My evidence for this is that if I leave the lines as they are,
gcc says:
* sw.c:19: error: expected identifier or '(' before '&' token
this code clean compiles with VCC:

***********************************************

typedef enum
{
SwitchIn_Neutral,
SwitchIn_Active

} SwitchIn_value_t;
typedef enum
{
VALUE_FULLVALID,
VALUE_UNAVAIL,
VALUE_INACCURATE,
VALUE_DISABLED,
VALUE_ERROR
}valueState_t;
#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))
#define NEUTRAL_VALID
VALUE_TO_BYTE(SwitchIn_Neutral,VALUE_FULLVALID)
#define ACTIVE_VALID VALUE_TO_BYTE(SwitchIn_Active,VALUE_FULLVALID)
#define DEFAULT_ERROR VALUE_TO_BYTE(SwitchIn_Neutral,VALUE_ERROR)

char DL_SwitchIn_ConfTable[3][10] =
{
{NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_V ALID,DEFAULT_ERROR,
DEFAULT_ERROR,NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_E RROR,DEFAULT_ERROR},
{NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ ERROR,NEUTRAL_VALID,
ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR,NEUTRAL_V ALID,ACTIVE_VALID},
{ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_V ALID,ACTIVE_VALID,
NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VA LID,NEUTRAL_VALID}
};
int main (void)
{
return 0;
}

***********************************************


are you certain you are showing us the real code?
--
Nick Keighley

"you'll never forget Alice's birthday"
Sep 18 '08 #5
Nick Keighley wrote:
#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))
#define NEUTRAL_VALID
VALUE_TO_BYTE(SwitchIn_Neutral,VALUE_FULLVALID)
OK, /your/ lines are broken here. (Tweaks newsreader to not wrap.
Curses newsreader than decides not to show the "read" messages,
just because of tweaking. Refinds Nick's article.) I don't think
it's my newsreader doing it. Hence my evidence that Rohit's
code lines are broken is weakened. All I have left is "it
compiles when that line is not broken" ...

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Sep 18 '08 #6
On Sep 18, 3:27*pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On 18 Sep, 11:10, Rohit <papakap...@gmail.comwrote:
On Sep 18, 2:56*pm, Chris Dollin <chris.dol...@hp.comwrote:
Rohit wrote:
#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))
You should note that this restricts both of your enums to values
less than 16, or rather that the values are taken mod 16 by your
macro. (So you can't just go on adding values without fixing the
macro. I'd suggest making sure your unit tests protect you against
this as far as they can.)
Its done intentionally since these enums have an upper limit of 10.
But I am getting error saying 'expected an expression' at array
initialization.
I suspect that your macro really is broken across two lines, as
it appears above, which means that the expansions are not legal
expressions.
Macro is not broken its continuous and on a single line. I guess it
exceeded browser window width so it appears broken.
My evidence for this is that if I leave the lines as they are,
gcc says:
* sw.c:19: error: expected identifier or '(' before '&' token

this code clean compiles with VCC:

***********************************************

typedef enum
{
* * SwitchIn_Neutral,
* * SwitchIn_Active

} SwitchIn_value_t;

typedef enum
{
* VALUE_FULLVALID,
* VALUE_UNAVAIL,
* VALUE_INACCURATE,
* VALUE_DISABLED,
*VALUE_ERROR

}valueState_t;

#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))
#define NEUTRAL_VALID
VALUE_TO_BYTE(SwitchIn_Neutral,VALUE_FULLVALID)
#define ACTIVE_VALID * VALUE_TO_BYTE(SwitchIn_Active,VALUE_FULLVALID)
#define DEFAULT_ERROR *VALUE_TO_BYTE(SwitchIn_Neutral,VALUE_ERROR)

* * char *DL_SwitchIn_ConfTable[3][10] =
* * * * {

{NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_V ALID,DEFAULT_ERROR,

DEFAULT_ERROR,NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_E RROR,DEFAULT_ERROR},

{NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ ERROR,NEUTRAL_VALID,

ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR,NEUTRAL_V ALID,ACTIVE_VALID},

{ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_V ALID,ACTIVE_VALID,

NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VA LID,NEUTRAL_VALID}
* * * * };

int main (void)
{
* * return 0;

}

***********************************************

are you certain you are showing us the real code?
Oops... There was a special character embedded in initialization. I
caught it by enabling editor feature.
It compiles properly. Thanks for informing that code is clean.
Rohit
Sep 18 '08 #7
On 18 Sep, 11:41, Chris Dollin <chris.dol...@hp.comwrote:
Nick Keighley wrote:
#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))
#define NEUTRAL_VALID
VALUE_TO_BYTE(SwitchIn_Neutral,VALUE_FULLVALID)

OK, /your/ lines are broken here. (Tweaks newsreader to not wrap.
Curses newsreader than decides not to show the "read" messages,
just because of tweaking. Refinds Nick's article.) I don't think
it's my newsreader doing it. Hence my evidence that Rohit's
code lines are broken is weakened. All I have left is "it
compiles when that line is not broken" ...
bother! why I didn't put a \ in I know not!
The version I intenbded to compile had the line repaired.
--
Nick Keighley
Sep 18 '08 #8
On 18 Sep, 11:43, Rohit <papakap...@gmail.comwrote:

<snip>
Oops... There was a special character embedded in initialization. I
caught it by enabling editor feature.
It compiles properly. Thanks for informing that code is clean.
it's a bugger when that happens

--
Nick Keighley
Sep 18 '08 #9
Rohit <pa********@gmail.comwrites:
I am trying to initialize an array whose initializers depend on value
of Enums. I take enum and then decide the initializer value, so that
even if enum value changes because of addition to list even then I
should be able to get correct value for the array element. I need
value and state to be present in a single byte thats why I use macros.
In a followup you wrote that the problem was a stray special character
in your source file. Glad you found the problem.

But I have another question about your code.
Here is what my code look like:

typedef enum
{
SwitchIn_Neutral,
SwitchIn_Active

} SwitchIn_value_t;

typedef enum
{
VALUE_FULLVALID,
VALUE_UNAVAIL,
VALUE_INACCURATE,
VALUE_DISABLED,
VALUE_ERROR
}valueState_t;
#define VALUE_TO_BYTE(SwitchValue,Valuestate) (((0x0F
&(SwitchValue))<< 4)|(0x0F & (Valuestate)))
#define NEUTRAL_VALID VALUE_TO_BYTE(SwitchIn_Neutral,VALUE_FULLVALID)
#define ACTIVE_VALID VALUE_TO_BYTE(SwitchIn_Active,VALUE_FULLVALID)
#define DEFAULT_ERROR VALUE_TO_BYTE(SwitchIn_Neutral,VALUE_ERROR)

char DL_SwitchIn_ConfTable[3][10] =
{
[snip]
};
Why are you using char rather than unsigned char? Type char may be
either signed or unsigned, depending on the implementation. This
could make a difference if your SwitchIn_value_t has more than 8
members.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 18 '08 #10

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

Similar topics

4
by: Diogo Alves - Software Developer | last post by:
I have the following code line: dsEmps.Tables.Columns.Expression = "MonthlyDeviation%60"; but hits gives an error it says that can't MOD from System.Double to System.Int32 I tried to put the...
58
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.