PS:
in Arduino world camelCase is also the standard notation so your function could be named initializeButton(..) and struct types are usually denoted with a capital letter so struct Button {...
No, initialization of the data members is the job of the constructor, not some separate function.
In this case you don't even need a constructor, just use aggregate initialization.
Even if you do need to pass the struct to a function, pass it by value or by reference. Only pass by pointer if it can be null. In this example, you could just return by value, no need for the cumbersome pointer output parameter.
You should not repeat the struct every time you use the struct type.
I remembered vaguely being plagued by this. In 'C', you have to repeat the 'struct' qualifier.
The typedef was just missing the name for the newly introduced type.
# include <stdio.h>
# include <stdlib.h>
typedef struct button {
unsigned int x;
unsigned int y;
unsigned int color;
} myButtonType;
myButtonType b2; // using the typedef
struct button b1; // you can say 'struct' if you want
button b3; // but you have to if it's just C.
int main() {
b1.color = 42;
b2.x = 314;
b3.y = 272;
printf("\n%d\n", b2.x);
}
There's nothing stopping you from using the same name for the struct tag and the typedef:
typedef struct button {
unsigned int x;
unsigned int y;
unsigned int color;
} button;
That's valid in both C and C++, and you can use either struct button or button (though I would capitalize it as Button in both cases, as J-M-L mentioned in Warning: 'typedef' was ignored in this declaration - #4 by J-M-L, to make it clear that it's a type rather than an object). You could even leave out the struct tag so you can use it only as button and not as struct button.
Yes, I'm noticing different methods for naming in different libraries; some in C and some in C++.
I mostly find both camelCase and all lower case with underscore for spacing. But I believe the camel case is the most one in Arduino world. The other one can be found in lower level C libraries if I'm correct here.
Yeah, but I don't know it the main principles of declaring and defining structs in C and C++ are the same. In your example, the variable name will be the only instantiated variable of the struct, so I wanted to instantiate another struct variable then I either set it with the first variable or set it later elsewhere; like:
struct GROUPS{
uint16_t a1;
uint16_t a2;
} GROUP1, GROUP2; // <-- declaring two variables of GROUPS struct
struct GROUPS GROUPA; // <-- declaring a variable GROUPA of GROUPS struct
struct GROUPS GROUPB; // <-- declaring a variable GROUPB of GROUPS struct
I was a little bit away of using structs so I forgot how to use the struct tag and the variable. And which one is the most important. I went back to my C compiler and did couple tests:
CASE 1: not using typedefs
Tag name is important for declaring other variables.
Not using a tag name is ok, but can't declare other variables.
CASE 2: using typedefs
Using tag name is not important for declaring other variables. But the variable name is now the type name so I must use it to declare variables.
Not using tag name is ok, but the variable name is a must to declare variables.
struct GROUPS{
uint16_t a1;
uint16_t a2;
} GROUP1, GROUP2; // <-- declaring two variables of GROUPS struct
GROUPS GROUPA; // <-- declaring a variable GROUPA of type GROUPS
GROUPS GROUPB; // <-- declaring a variable GROUPB of type GROUPS
In my example I didn't instantiate any variables. button is both a struct tag (use it as struct button in C or C++ or as just button in C++) and a typedef (use it as button in C or C++).