const and struct pointers

Elliott Finley efinley%ug.utah.edu at cs.utah.edu
Mon Feb 26 08:34:29 AEST 1990


(Charles Hannum) writes:

     The double is passed by value; so dereferencing it works fine.
     But the struct is passed by reference (as are *all* structures in
     C!).  In reality, you need to pass a "struct qwert *" to the
     function.  Normally, the compiler takes the reference
     automatically, but you are trying to do this in reverse.  Thus,
     it does not work; you simply can't pass a structure by value.

Here is an excerpt from K&R2, chapter 6, page 127
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
     The main change made by the ANSI standard is to define structure
assignment--structures may be copied and assigned to, passed to
functions, and returned by functions.  This has been supported by most
compilers for many years, but the properties are now precisely
defined.  Automatic structures and arrays may now also be initialized.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Chapter 6.2 page 129

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
     Let us investigate structures by writing some functions to
manipulate points and rectangles.  There are at least three possible
approaches: pass components separately, pass an entire structure, or
pass a pointer to it.  Each has its good points and bad points.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

     So you can clearly see that the statement made above "But the
struct is passed by reference (as are *all* structures in C!)" is not true!

                    Elliot

P.S. It is ALOT more efficient to pass a pointer to a structure.



More information about the Comp.lang.c mailing list