Hi All
I am studying structs at the moment and according to the tutorial in the link below (from minute 14), there are a number of ways to access and modify structs.
Also I am trying to use String instead of using a char array for the name type, but I get errors when using strcpy... yes I am aware that I can simply do clare.name = "Clare"; when using String rather than char but I wander... is strcpy only working with char arrays and not with strings ??
example:
struct students{
char name[25];
int age;
int rollno;
};
struct students john {"John", 24, 555};
struct students clare, bob;
// struct students mark { name : "Mark", age : 24, rollno : 566 };
// struct students mark { .name = "Mark", .age = 24, .rollno = 566 };
void setup()
{
clare.age = 55;
strcpy(clare.name, "Clare");
}
void loop() {
}
That's an open bug: designated initializer for char array by string constant. Found that by searching for the error message; first result was in StackOverflow. One reply says that it is fixed in a certain version of the compiler, but you really don't have much control over when that will trickle down to what your board is using.
There's a workaround mentioned in Bugzilla, but the Arduino language server doesn't like it, and marks it as an error if you have Real Time Diagnostics enabled.
You can just do
mark.name = "Mark";
in a separate statement. A little more typing; compiled code is nearly the same.
I am not getting the result you are getting . I am using wokwi right now and getting this error:
sketch.ino:11:49: sorry, unimplemented: non-trivial designated initializers not supported
struct students mark { age : 24, rollno : 566 };
Thanks for this clarification.
I have also just discovered how to make a struct variable "read only" const students john {"John", 24, 555}; but I must say I am getting frustrated with the fact that I am trying to improve my knowledge of C but keep getting compilation errors when trying stuff on Wokwi simulating an arduino uno... for example the line to assing a memory address to a pointer to a struct (ptr = &john;) will give an error...
I think the cause of the errot is that you put this line somewhere before setup(), outside the any function. Only initializations and defines can be put there.
The line ptr = &john; is not an initialization, it is an assignment. The assignment is allowed only inside the function.
I am aware of this and I am keeping an eye on both languages when I find things that do not work. On this occasion I noticed that the syntax was correct in C++. But now we know what I did wrong...