468,291 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

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 10265
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 discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.