I have a structured defined as
typedef struct{
char string1
char string2
int number1
char string3
}structure1
and want to assign the values to string1,string2,number1,string3 in a loop like this
structure1 bob
for(int i = 0,i<=4,i++)
{
bob.i = assigned value
}
now I understand that code above in it’s generic form will only work for integers as you can’t just go string = string for assignment, but the same problem arises as I don’t know how to reference the values inside a struct without specifically naming them one by one. for strings there will be a second assignment relying on the i index to work out if it’s a integer or string at the time so it can perform the assignment. I was thinking something along the lines of enum’s but I’ve never used them in a practical sense before, just theoretical.
It’s not possible in C. The closest to that would be calculating field offsets and then using them to assign values:
Warning: this code is just to demonstrate it is possible to assign fields without their name, but you should not use it!