How does C handle copying structs (not pointer to structs) with the assignment operator. I have a sample program below demonstrating my question.
struct s {
char string[20];
};
void main() {
struct s var1, var2;
strcpy(var1.string, "hello");
printf("var1: %s\n", var1.string);
printf("var2: %s\n", var2.string);
var2 = var1;
printf("var1: %s\n", var1.string);
printf("var2: %s\n\n", var2.string);
strcpy(var2.string, "goodbye");
printf("var1: %s\n", var1.string);
printf("var2: %s\n", var2.string);
}
The output I expect is first "var1: hello var2:" since var2.string is nothing.
The second block should be "var1: hello var2: hello", since var1 and var2 are the same.
The third block should be "var1: goodbye var2: goodbye", since var1 and var2 should be the same memory location.
What I get for the third block, though, is "var1: hello var2: goodbye". So it looks like the line var2 = var1
sets all of the attributes of var2 to the attributes of var1 automatically. Is this what C does instead of simply decomposing them to locations in memory?