Ways to access structs

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.

https://www.youtube.com/watch?v=y4x3iyESFgs&list=PLfVsf4Bjg79CZ5kHTiQHcm-l2q8j06ofd&index=59)
however two of the ways he describe (the ones I have commented out) don't seem to work in arduino code. I just checking if this is normal or maybe I am missing something.

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 ?? :thinking:

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() {
}

What is not work in this code for you?

yes, the strcpy() finction is for char arrays only, and all other strxxx functions (strcmp, strlen... etc) as well.

the lines above do not compile

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.

Thanks a lot for that!! I couldn't understand if it was me or if there is stuff in C that arduino doesn't like...

Just an addition to @kenb4 answer - the only char field name is a problem, you can easily use both syntaxes to initialize the other fields:

 struct students mark {  age : 24, rollno : 566 };
 struct students dave {  .age = 28, .rollno = 66 };

and then add the name as additional line.

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 };

What the board did you selected while the compile the code? Arduino IDE uses a different compilers depending on board.

I used a arm-none-eabi-gcc v9.2.1 compiler for STM32F103 mcu

I am simulating with a UNO

Confirm, it is not compiled for Uno.
Sorry

Thanks!! :slightly_smiling_face:

Hello fluxia

Check and study this small example to gain the knowledge.

struct students {
  char name[25];
  int age;
  int rollno;
};

students john {"John", 24, 555};
students clare {"clare", 24, 555};
students bob {"bob", 24, 555};

void setup()
{
  Serial.begin(115200);
  
  clare.age = 55;
  strcpy(clare.name, "Clare");

  bob.age = 23;
  strcpy(bob.name, "Bob");

  Serial.print(john.name),  Serial.print(" - "), Serial.print(john.age),  Serial.print(" - "), Serial.println(john.rollno);
  Serial.print(clare.name),  Serial.print(" - "), Serial.print(clare.age),  Serial.print(" - "), Serial.println(clare.rollno);
  Serial.print(bob.name),  Serial.print(" - "), Serial.print(bob.age),  Serial.print(" - "), Serial.println(bob.rollno);
}

void loop() {
}

Have a nice day and enjoy coding in C++.

Note that this "designated initializer" feature is only useful for initializations, anyway. It's not generally useful for "access to a struct."

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... :face_with_head_bandage:

struct students *ptr;
ptr = &john;

error: 'ptr' does not name a type
ptr = &john;

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.

Try to move it inside the setup()

struct students *ptr;

void setup() {
ptr = &john;
...
}

and it will compiled without errors.

The other way to put this lines together:

struct students *ptr = &john;

You are right! what a silly mistake :slight_smile:

That's because you're looking at the wrong language: Arduino code is C++, not C.

Designated initializers are a C++20 feature, while the Arduino Uno is still (artificially) stuck on C++11.

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... :blush: