Array initialization error "expected an expression"

R

Rohit

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_VALID,DEFAULT_ERROR,

DEFAULT_ERROR,NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR},

{NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR,NEUTRAL_VALID,

ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR,NEUTRAL_VALID,ACTIVE_VALID},

{ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,

NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,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?
 
C

Chris Dollin

Rohit said:
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?".
 
R

Rohit

Rohit wrote:

(fx:snip)


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)


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
 
C

Chris Dollin

Rohit said:
Its done intentionally since these enums have an upper limit of 10.

I suggest comments to that effect, as well as tests.
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.
 
N

Nick Keighley

Its done intentionally since these enums have an upper limit of 10.



Macro is not broken its continuous and on a single line. I guess it
exceeded browser window width so it appears broken.

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_VALID,DEFAULT_ERROR,


DEFAULT_ERROR,NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR},


{NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR,NEUTRAL_VALID,


ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR,NEUTRAL_VALID,ACTIVE_VALID},


{ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,


NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID}
};


int main (void)
{
return 0;
}

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




are you certain you are showing us the real code?
 
C

Chris Dollin

Nick said:
#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" ...
 
R

Rohit

Its done intentionally since these enums have an upper limit of 10.
Macro is not broken its continuous and on a single line. I guess it
exceeded browser window width so it appears broken.

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_VALID,DEFAULT_ERROR,

DEFAULT_ERROR,NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR},

{NEUTRAL_VALID,ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR,NEUTRAL_VALID,

ACTIVE_VALID,DEFAULT_ERROR,DEFAULT_ERROR,NEUTRAL_VALID,ACTIVE_VALID},

{ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,

NEUTRAL_VALID,ACTIVE_VALID,NEUTRAL_VALID,ACTIVE_VALID,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
 
N

Nick Keighley

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.
 
N

Nick Keighley

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
 
K

Keith Thompson

Rohit said:
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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top