Array initialization error "expected an expression"

Discussion in 'C Programming' started by Rohit, Sep 18, 2008.

  1. Rohit

    Rohit Guest

    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?
     
    Rohit, Sep 18, 2008
    #1
    1. Advertisements

  2. Rohit

    Chris Dollin Guest

    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)
    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?".
     
    Chris Dollin, Sep 18, 2008
    #2
    1. Advertisements

  3. Rohit

    Rohit Guest

    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.
    Rohit
     
    Rohit, Sep 18, 2008
    #3
  4. Rohit

    Chris Dollin Guest

    I suggest comments to that effect, as well as tests.
    Not as it's sent, it isn't.
    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.
     
    Chris Dollin, Sep 18, 2008
    #4
  5. 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?
     
    Nick Keighley, Sep 18, 2008
    #5
  6. Rohit

    Chris Dollin Guest

    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" ...
     
    Chris Dollin, Sep 18, 2008
    #6
  7. Rohit

    Rohit Guest

    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
     
    Rohit, Sep 18, 2008
    #7
  8. 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, 2008
    #8
  9. it's a bugger when that happens
     
    Nick Keighley, Sep 18, 2008
    #9
  10. 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.
    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, Sep 18, 2008
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.