On 26/09/2008, at 5:36 AM, Erick Garske wrote:

I can see merit in both techniques.

Simon, did you mean to pass the structure by value or by reference?

By reference, hence the myEC_t * in the function interface and the use of "indirection operator" in my response.

I suppose that this would depend on whether or not the original value
from the structure needs to be preserved, or if myFunction modifies
the structure values, and those values need to be returned back to the
structure.

You could use that to decide whether value or reference is most appropriate but you also need to consider convention. When you pass a structure you are making the compiler copy it. Most C programmers would consider that to be unnecessary use of CPU. They'd also consider passing a structure by value to be very odd.

I'm not in that camp--there are advantages to passing structures by value but you need to KNOW that's what you intend. I'll even return structures from functions if it makes sense. I've also used structures to attempt to provide fixed-length field interfaces for OS APIs.

The thing to remember is that C passes EVERYTHING, except arrays, by value. The only reason C passes arrays by reference is that it doesn't really have arrays. It makes no distinction between an array subscript and a pointer offset. Thus an array an a pointer are the same thing in C and you can use the two types of syntax interchangeably. While this can occasionally prove useful it is also one of the crappy things about C. Because arrays are just pointers it does no bounds checking. Because it has no concept of fixed-length fields (they are just arrays of characters) you can reference beyond the declared size. In some C compilers you can even reference before the start of the field. Easy area to shoot yourself in the foot and overwrite storage you don't own.

Regards,
Simon Coulter.
--------------------------------------------------------------------
FlyByNight Software OS/400, i5/OS Technical Specialists

http://www.flybynight.com.au/
Phone: +61 2 6657 8251 Mobile: +61 0411 091 400 /"\
Fax: +61 2 6657 8251 \ /
X
ASCII Ribbon campaign against HTML E-Mail / \
--------------------------------------------------------------------




This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2019 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].