472,129 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,129 software developers and data experts.

why cann't compare the structures?

If i take two structures with same members then,why cann't i compare
those two structures?
Oct 14 '08 #1
4 9972
DDD
On Oct 14, 10:06*am, Google Groups <mohasinb...@gmail.comwrote:
If i take two structures with same members *then,why cann't i compare
those two structures?
You can do it. But you have to write it youself. In c, maybe a
function takes two arguments can solve this problem.

If in C++, you could write a class(struct) like a std::string, and
overload operation such as ==, and <.

Good luck!
Oct 14 '08 #2
On 14 Oct, 03:06, Google Groups <mohasinb...@gmail.comwrote:
If i take two structures with same members *then,why cann't i compare
those two structures?
You can.

struct foo;
extern struct foo * new_foo( int );
extern int compare( struct foo *, struct foo *);

int
main( void ) {
struct foo *a;
struct foo *b;

a = new_foo( 67 );
b = new_foo( 23 );
compare( a, b );
return 0;
}
Oct 14 '08 #3
Google Groups wrote:
If i take two structures with same members then,why cann't i compare
those two structures?
You can't compare them using `==` because the language doesn't
do that.

It probably doesn't do it because (a) it can hide a lot of
operations under an innocent-looking `==` symbol, and C
traditionally doesn't conceal that much activity, and (b) it
isn't clear what the meaning of that `==` should be, and (c)
it's sufficiently easy to write one's own comparison function
that the pressure is off the language.

Re (a), one might think that was good reason to also disallow
assignment. However that seems to be significantly more useful
an operation, is required for passing structures by value (which
is less convenient to do by hand than comparision is), and makes
it a trifle easier for the compiler to generate good code for
copying one struct to another.

Re (b), consider

struct amby { int n; char *cp; } a, b;
...
if (a == b) ...

Presumably you expect the compiler to check that a's and b's
`n` fields are `==`. What about the `cp` fields? How shall it
compare them?

Whichever of the at-least-two ways you suggest, some people
will expect the other. The compiler in general /doesn't know/
what equality should mean on a user-defined data type, and
C (unlike say Pop11, C++, or Java) has no machinery for attaching
a user-defined function to a standard equality test.

Re (c), note the minor complication that it may make a significant
difference how the structs are passed to the comparision function,
by pointer or by value, and the difference is forced to be visible
in the calling code. Unlike (say) C++ or (ISO) Pascal, you can't
change your mind post-hoc without changing all the uses of the
function. Opinions on whether this is a Good Thing or not differ.

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Oct 14 '08 #4
In article <gd**********@news-pa1.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrote:
Google Groups wrote:
If i take two structures with same members then,why cann't i compare
those two structures?

You can't compare them using `==` because the language doesn't
do that.

It probably doesn't do it because (a) it can hide a lot of
operations under an innocent-looking `==` symbol, and C
traditionally doesn't conceal that much activity, and (b) it
isn't clear what the meaning of that `==` should be, and (c)
it's sufficiently easy to write one's own comparison function
that the pressure is off the language.
[...]
Re (b), consider

struct amby { int n; char *cp; } a, b;
...
if (a == b) ...

Presumably you expect the compiler to check that a's and b's
`n` fields are `==`. What about the `cp` fields? How shall it
compare them?
We can even restrict ourselves to non-pointer members:

struct S { char s [256]; };

S s1 = { 'f','o','o',0,'b','a','r' };
S s2 = { 'f','o','o',0 };

assert( s1 == s1 ); // passes or fails?

Here, s1 and s2 both have the same C-style string in s, but the contents
of the entire arrays differ. You probably only want it to compare up to
the nul terminator if you're using s as a string, but ALL elements if
you're not storing strings.

If one wants operator overloading, there's already a nearly-C-compatible
language that provides it.
Oct 15 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Evgeny Gopengauz | last post: by
1 post views Thread by Evgeny Gopengauz | last post: by
1 post views Thread by Xiao Quan | last post: by
1 post views Thread by Stephen | last post: by
3 posts views Thread by Kiran B. | last post: by
4 posts views Thread by Nie longhai | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.