- With regular primitive types we have a wide variety of operations
available, including assignment, comparisons, arithmetic, etc.
- Most of these operations would NOT make sense on structures.
Arithmetic and comparisons, for example:
Student s1, s2;
s1 = s1 + s2; // ILLEGAL!
// How would we add two students together, anyways?
if (s1 < s2) // ILLEGAL. What would this mean, anyways?
// yadda yadda
- HOWEVER, using the assignment operator on structures IS legal,
as long as they are the same type. Example (using previous struct
definitions):
Student s1, s2;
Fraction f1, f2;
s1 = s2; // LEGAL. Copies contents of s2 into s1
f1 = f2; // LEGAL. Copies f2 into f1
- Note that in the above example, the two assignment statements are
equivalent to doing the following:
strcpy(s1.fName, s2.fName); // these 4 lines are equivalent to
strcpy(s1.lName, s2.lName); // s1 = s2;
s1.socSecNumber = s2.socSecNumber;
s1.gpa = s2.gpa;
f1.num = f2.num; // these 2 lines are equivalent to
f1.denom = f2.denom; // f1 = f2;
Clearly, direct assignment between entire structures is easier, if
a full copy of the whole thing is the desired result!