2

I am trying to assign values to an array within a typedef struct and continually am getting an syntax error.

Error expected '=', ',', ';', 'asm' or '__attribute__' before '.' token

Here is my code:

myfile.h

#define  Digit12  0x00u
#define  Digit34  0x01u
#define  Digit56  0x01u

typedef struct
{
   uint8_t trData[3]; 
} CImageVersion;

myfile.c

CImageVersion oImageVersion; // declare an instance

oImageVersion.trData = { Digit12, Digit34, Digit56};

Later on in the code

otherfile.c

extern CImageVersion oImageVersion;

An arry is a pointer but if i change the assignment to

oImageVersion->trData = { Digit12, Digit34, Digit56};

I get the same error. I am very confused as to what I am doing wrong The error is pointing to directly after the oImageVersion when I assign the values

4
  • 1
    You can't assign to an array. Only initialize it on definition or copy to it. Commented Oct 23, 2017 at 17:18
  • Try CImageVersion oImageVersion; // declare an instance oImageVersion.trData = { Digit12, Digit34, Digit56}; --> CImageVersion oImageVersion = { {Digit12, Digit34, Digit56} }; Commented Oct 23, 2017 at 17:19
  • Also, -> is for pointers to structures. Actual structures use . Commented Oct 23, 2017 at 17:38
  • 2
    Rather than assign the array, assign the structure: oImageVersion = (CImageVersion) { {Digit12, Digit34, Digit56} }; Commented Oct 23, 2017 at 17:41

1 Answer 1

3

You can't assign directly to an array. The syntax you're using is only valid when a variable is defined. I.e. you can do this:

CImageVersion oImageVersion = { { Digit12, Digit34, Digit56} };

But not this:

CImageVersion oImageVersion;
oImageVersion.trData = { Digit12, Digit34, Digit56};

If you don't assign the values when the variable is defined, you need to assign to each array element individually:

oImageVersion.trData[0] = Digit12;
oImageVersion.trData[1] = Digit34;
oImageVersion.trData[2] = Digit56;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.