473,381 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 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 10110
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: inna | last post by:
Hello. I have an information question. In my work I often have to compare databases between releases in order to upgrade them to needed version of software we developing. And I am looking for...
6
by: Evgeny Gopengauz | last post by:
Let us suppose that I have two similar databases and need to create an sql-script upgrating one database structure to another. For example, these databases are from different versions of some...
1
by: Evgeny Gopengauz | last post by:
Hello all! My question is: how to compare two database structures if I only have their DDL files? As the result I would expect an sql-script upgrating one database structure to another. The...
1
by: Xiao Quan | last post by:
i programed a java webservice, and a client by MS SOAP Toolkit. it work well. Howerver, a .net client cann't access the java webservice and report the following exception: unhandled...
1
by: Stephen | last post by:
I am trying to compare the tables in two similar databases using the SQLDMO object. I am able to use this object to access different SQL servers and choose two different databases. The versions of...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
4
by: Nie longhai | last post by:
I have a generic method in a class as follow: T Get<T>(string key) { ..... return Convert.ChangeType(v, typeof(T)) } when I use it in my code:
6
nehashri
by: nehashri | last post by:
hello im preparing a chemical database where i need to store chemical structures. ASP as front end i want that when the user draw a structure(user will manually draw the structure) in frontend...
3
by: ago | last post by:
Hi, Is there any way to compare two identical table structures in access for different values in them. EG: Table 1: Name occupation rob plumber Table 2: Name occupation
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.