Search code examples
arrayscinitializationdeclarationdefinition

"error expected expression before '{' token"


I'm trying to declare global extern array and define it in another file but error is given. Please find the code snippet below

in .h

extern unsigned int HoldingRegisters[2];

in .c

HoldingRegisters={2,0};

"error expected expression before '{' token"

Thank you in advance


Solution

  • This record in the header file in.h

    extern unsigned int HoldingRegisters[2];
    

    is a declaration of an array but not its definition.

    In the file in.c apart from the declaration you need also to define the array like

    unsigned int HoldingRegisters[2] = {2,0};
    

    This definition must be placed in the file scope outside any function as for example

    #include "in.h>
    
    unsigned int HoldingRegisters[2] = {2,0};
    
    //...
    

    Otherwise this record that you shall remove)

    HoldingRegisters={2,0};
    

    represents an attempt to assign a braced list to the variable HoldingRegisters of the array type. However arrays are non-modifiable lvalues. That is they do not have the copy assignment operator and moreover in an assignment you need to use an expression and braced lists are not expressions.