cancel
Showing results for 
Search instead for 
Did you mean: 

I don't understand why STM32CubeIDE give me this syntax error. How do you access to array that are inside a struct?

William F
Associate II

typedef struct{

uint8_t data[10];

uint8_t time[10];

uint8_t description[10];

} FileString;

FileString Datalogger_FileString;

Datalogger_FileString.data[0]=0; //ERROR: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token

What do I should do to access the array inside a struct? Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

Your code snippet compiles fine. May be some stray non-ASCII chars if you copy-pasted the code from elsewhere.

Edit: The above holds, but I automatically assumed that the last two lines appeared later in some function.

You cannot put assignments (statements) at global level,unlike in Python for example.

You could init with

FileString Datalogger_FileString1 = { .data[0]=0 };

But since all globally defined variables are initialized to 0 by the C runtime, there is no effect in doing so.

hth

KnarfB

View solution in original post

2 REPLIES 2
KnarfB
Principal III

Your code snippet compiles fine. May be some stray non-ASCII chars if you copy-pasted the code from elsewhere.

Edit: The above holds, but I automatically assumed that the last two lines appeared later in some function.

You cannot put assignments (statements) at global level,unlike in Python for example.

You could init with

FileString Datalogger_FileString1 = { .data[0]=0 };

But since all globally defined variables are initialized to 0 by the C runtime, there is no effect in doing so.

hth

KnarfB

Thank you. I notice that the syntax depends on where you initialize the variable. As global, you must initialize as you said:

FileString Datalogger_FileString1 = { .data[0]=0 };

If you are inside a function you can init as I do.

Datalogger_FileString.data[0]=0;

Thank you