473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 11120
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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 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.