Structure Assignments
807578May 5 2004 — edited May 28 2004Here is my question for Sun's C gurus:
------------------------------------------------------------------
I want to copy the contents of a structure into
another instance of the same structure.
Example:
struct mystruct
{
char field1[20];
char field2 [30];
int field3;
};
Given two instances of this structure:
struct mystruct a;
struct mystruct b;
Careful way to copy contents of a into b would be:
strcpy(b.field1,a.field1);
strcpy(b.field2,a.field2);
b.field3 = a.field3;
But I have tried this method:
b = a;
Compiled under our Sun OS, this works fine.
Questions: Am I justified in treating structures
as "true" datatypes, like ints, etc? Can I be confident
that doing a value assignment like that above
will always work? How portable is this? Is it part
of ANSI C?
I want to know if I should really avoid structure
assignment like this in an application that must
be fairly robust, and portable.
----------------------------------------------------------