Looking for memcpy() and memcmp() routines in C

Doug Gwyn gwyn at smoke.BRL.MIL
Sun Feb 26 15:40:32 AEST 1989


In article <20598 at tiger.oxy.edu> bagpiper at oxy.edu (Michael Paul Hunter) writes:
>In article <463 at sunset.MATH.UCLA.EDU> tony at joshua.math.ucla.edu writes:
>>Can somebody e-mail me C routines that copies (or compares) two
>>structures of the same type?
>If your compiler will pass structures you can create a routine like the
>one which follows to copy structures.

The easiest way to copy a struct is to use assignment (=).

>I think there might be a probablem in comparing byte by byte in that
>their could be unused space (well it is used for alignment) which might
>contain different values even though two different structure are the same
>member for member.

Not only that, but if you're testing for anything but exact equality
you'd probably get the wrong results by comparing bytes instead of the
actual object members.  Also note that string members should be
compared via strcmp() and not just for pointer equality or as arrays.

>What does the proposed ANSI spec say (BTW, when well it not be proposed
>anymore?) and what do older compilers do?

The pANS requires support for struct assignment, struct function parameters,
and struct function return values.  These have actually been implemented in
most C compilers since around 1978.  Struct comparison is not supported,
because there is no unambiguously correct way to do it, as noted above.
The application itself must implement whatever is appropriate for this.

The proposed ANSI C standard is being reviewed by X3, with balloting to
end early in March.  If X3 approves it, the proposed standard will then
be sent to ANSI and it is unlikely in the extreme that it would not be
accepted as the official ANS for C at that point.



More information about the Comp.lang.c mailing list