Structures

Rick Schaut schaut at cat9.cs.wisc.edu
Sun Mar 4 06:03:29 AEST 1990


In article <14302 at reed.UUCP> minar at reed.bitnet (Nelson Minar) writes:
| 
| My copy of K&R 1 (I'll be getting 2 soon..) says
| 
| "There are only two things that can be done with a structure or a union:
| name one of its members (by means of the . operator); or take its address
| (by unary &).  Other operations, such as assigning from or to it or passing
| it as a parameter, draw an error message.  In the future, it is expected
| that these operations, but not necessarily others, will be allowed."
| 
| 1] What does ansi say about this?

According to K&R 2, p129:

	The only legal operations on a structure are copying it or assigning
	to it as a unit, taking its address with &, and accessing its members.
	Copy and assignment include passing arguments to functions and
	returning values from functions as well.

| 2] It seems that passing a structure-by-value is illegal under this rule.
| Why this limitation in the original C?

Because C was originaly written for a DEC PDP11 which only had 64K of
ram.  Under those conditions, passing structures by value simply takes
up too much stack space.

| 3] My C compiler (Turbo C 2.0) doesn't mind passing a structure-by-value,
| and it allows you to set two structures equal to each other, automatically
| copying the elements. Is this standard behavior for most C compilers? Why
| does Turbo C have an <optional> warning "Warning: structure passed by value"
| if it is legal TC 2.0 code? Because it is potentially non-portable?

It's a warning because most people (including me) still try to restrict
themselves to passing only pointers to structures.  I'm not even sure why
you'd ever want to pass a structure by value.  Note that it is a _warning_,
not an _error_, message which means that the code isn't necessarily illegal,
only that it has a _potential_ error.

--
Rick (schaut at garfield.cs.wisc.edu)

"I'm a theory geek; we use Turing machines!"--Gary Lewandowski



More information about the Comp.lang.c mailing list