T
T
Taras Parashchuk2020-09-27 21:25:54
C++ / C#
Taras Parashchuk, 2020-09-27 21:25:54

Why does an error occur when accessing a struct?

I use Atme Studio
The structure is this:

struct button {
char *button_port;
uint8_t button_pin;
uint8_t flagPress;
uint8_t flagClick;
uint8_tbuttonCount;
uint8_t time_button;
} button_left, button_right, button_menu;

It is necessary to set the initial parameters of the buttons.
It doesn't work like this: button_left.button_port = "PORTD"; writes
Error expected '=', ',', ';', 'asm' or '__attribute__' before '.' token

This is how it works:
struct button button_left = {"PORTD", 6, 0, 0, 0, 12};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-09-28
@taras1978

It doesn't work like this: button_left.button_port = "PORTD";
And this is how it works:
struct button button_left = {"PORTD", 6, 0, 0, 0, 12};
Why does an error occur when accessing a struct?

Do you see the difference between these two lines? The first generates code that assigns a value to one field of the structure. It should be where you can write code - in the body of some function. You can't write it outside of all functions.
The second generates an initialized structure. It can be written outside of all functions - then it will generate a globally visible structure initialized before the start of the program execution. It can also be written inside a function - then it will generate an automatic variable and initialize it at the moment of "execution".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question