brianh@hpcvia.CV.HP.COM (brian_helterline) (03/15/90)
I have a simple question:
Can you assign a struct like any other data type?
An example:
struct {
int a;
int b;
long c;
} var1, var2;
var1.a = 3;
var1.b = 4;
var1.c = 15L;
var2 = var1; /* <--- is this legal? */
I was told it was not. Is this always the case? What does ANSI say?
Thanks the info.
anigbogu@loria.crin.fr (Julian ANIGBOGU) (03/15/90)
In article <31530005@hpcvia.CV.HP.COM> brianh@hpcvia.CV.HP.COM (brian_helterline) writes: >I have a simple question: Can you assign a struct like any other data type? >An example: > >struct { > int a; > int b; > long c; > } var1, var2; > > var1.a = 3; var1.b = 4; var1.c = 15L; > > var2 = var1; /* <--- is this legal? */ >I was told it was not. Is this always the case? What does ANSI say? > >Thanks the info. Your assignment of var1 to var2 is perfectly legal !! I guess somebody has been reading old K&R lately. This is the second time in as many days that stucture assignments are creating problems. Any compiler that doesn't support this certainly needs a resting place in a museum! When in doubt about such problems and a C book is not handy, go ahead as you did above, add a print statement since you know what results you expect and see what your compiler does. I do that myself from time to time because there are certain problems that are not covered in textbooks. Evidently we can't expect authors to know about every possible usage of a particular C facility: it's normally applications that determine what facilities a programmer uses. Julian -- --------------------------------------- e-mail: anigbogu@loria.crin.fr | All opinions expressed here are | | naturally mine. However ... | ----------------------------------------
henry@utzoo.uucp (Henry Spencer) (03/16/90)
In article <31530005@hpcvia.CV.HP.COM> brianh@hpcvia.CV.HP.COM (brian_helterline) writes: >Can you assign a struct like any other data type? Yes. This has been possible since the V7 C compiler, circa 1979. Unfortunately K&R1 slightly predated this feature, so some compilers implemented based on the book don't do it. There is no longer any excuse for this. -- MSDOS, abbrev: Maybe SomeDay | Henry Spencer at U of Toronto Zoology an Operating System. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu
kohli@gemed (Jim Kohli) (03/16/90)
In article <31530005@hpcvia.CV.HP.COM>,brianh@hpcvia.CV.HP.COM (brian_helterline) writes: >I have a simple question: > >Can you assign a struct like any other data type? > > [example deleted] > >I was told it was not. Is this always the case? What does ANSI say? > I don't know what the ANSI standard says, but K&R (rev 1) does leave some reasonable doubt w.r.t. this functionality being *required*. From page 121: "This implies that structures may not be assigned to or copied as a unit, and that they can not be passed to or returned from functions. (These restrictions will be removed in forthcoming versions.)" SUN supports struct to struct assignments-- I would advise that you check on your machine with your test program (and some modifications to verify the results). Jim Kohli GE Medical Systems