Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > comparing two structures

Reply
Thread Tools

comparing two structures

 
 
Tor Rustad
Guest
Posts: n/a
 
      09-15-2007
CBFalconer wrote:
> Tor Rustad wrote:
>> CBFalconer wrote:
>>
>> [...]
>>
>>> Which should resolve to (very roughly}:
>>>
>>> for (c = eachcomponentof a) {
>>> if ((a->c) != (b->c)) return 0; /* unequal */
>>> }
>>> return 1; /* i.e. structures equal */
>>>
>>> Which means you have to write a cmp() each structure type. You
>>> may have to resolve embedded pointers.

>> Very confusing name choice, unless cmp() return 0 if equal.

>
> You have my full permission to interchange 0 and 1.


The API is still broken to my mind.

If first argument is less, then cmp() should return negative (e.g. -1),
if first argument is greater, then cmp() should return positive (e.g. 1).


Furthermore, if writing a cmp() function, I rather prefer to use the
same interface and return value as qsort() and bsearch() expect:

int cmp_mystruct(const void *, const void *)

why compare "struct's", unless doing sorting and searching?

--
Tor <torust [at] online [dot] no>
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      09-15-2007
Tor Rustad said:

<snip>

> Furthermore, if writing a cmp() function, I rather prefer to use the
> same interface and return value as qsort() and bsearch() expect:
>
> int cmp_mystruct(const void *, const void *)
>
> why compare "struct's", unless doing sorting and searching?


Well said, although I think I'd like to add an extra void * to that, so
that the comparison function can use (or provide) additional
information about the comparison without resorting to global data.
This, of course, requires ISO to change the spec for qsort and bsearch,
or provide sensibly-designed versions thereof.

Jump to it, lads...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      09-15-2007
Tor Rustad wrote:

> Furthermore, if writing a cmp() function, I rather prefer to use the
> same interface and return value as qsort() and bsearch() expect:
>
> int cmp_mystruct(const void *, const void *)
>
> why compare "struct's", unless doing sorting and searching?


If you had a linked list of structures
and if you had a list sorting function that
took a compar function pointer as an argument,
then it would make more sense to have pointers to the list node type.

--
pete
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-15-2007
Tor Rustad wrote:
> CBFalconer wrote:
>> Tor Rustad wrote:
>>> CBFalconer wrote:
>>>
>>> [...]
>>>
>>>> Which should resolve to (very roughly}:
>>>>
>>>> for (c = eachcomponentof a) {
>>>> if ((a->c) != (b->c)) return 0; /* unequal */
>>>> }
>>>> return 1; /* i.e. structures equal */
>>>>
>>>> Which means you have to write a cmp() each structure type.
>>>> You may have to resolve embedded pointers.
>>>
>>> Very confusing name choice, unless cmp() return 0 if equal.

>>
>> You have my full permission to interchange 0 and 1.

>
> The API is still broken to my mind.
>
> If first argument is less, then cmp() should return negative
> (e.g. -1), if first argument is greater, then cmp() should
> return positive (e.g. 1).


But you are dealing with a struct. How do you define 'less' when
there are multiple components, with no less/greater definition.

>
> Furthermore, if writing a cmp() function, I rather prefer to use
> the same interface and return value as qsort() and bsearch()
> expect:
>
> int cmp_mystruct(const void *, const void *)
>
> why compare "struct's", unless doing sorting and searching?


No, you can compare some fields of structs, but not complete
structs, for relative magnitude. The problem handled by void* is
not within the cmp function, but within the caller, who may not
need to know anything more than the cmp result about the struct.
The definition of cmp is up to you.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-15-2007
CBFalconer said:

<snip>

> But you are dealing with a struct. How do you define 'less' when
> there are multiple components, with no less/greater definition.


It depends on the domain. You are right that there is no general
solution, but that does not mean that there are no domain-specific
solutions. Nor does it mean that you cannot have multiple comparison
functions for the same type.

<snip>

> The definition of cmp is up to you.


Precisely.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      09-16-2007
CBFalconer wrote:
> Tor Rustad wrote:
>> CBFalconer wrote:
>>> Tor Rustad wrote:
>>>> CBFalconer wrote:
>>>>
>>>> [...]
>>>>
>>>>> Which should resolve to (very roughly}:
>>>>>
>>>>> for (c = eachcomponentof a) {
>>>>> if ((a->c) != (b->c)) return 0; /* unequal */
>>>>> }
>>>>> return 1; /* i.e. structures equal */
>>>>>
>>>>> Which means you have to write a cmp() each structure type.
>>>>> You may have to resolve embedded pointers.
>>>> Very confusing name choice, unless cmp() return 0 if equal.
>>> You have my full permission to interchange 0 and 1.

>> The API is still broken to my mind.
>>
>> If first argument is less, then cmp() should return negative
>> (e.g. -1), if first argument is greater, then cmp() should
>> return positive (e.g. 1).

>
> But you are dealing with a struct. How do you define 'less' when
> there are multiple components, with no less/greater definition.


Hmmm.. what does this "comparing struct" mean?

Normally, when comparing ADT, the ADT has a key defined, e.g. like this:

struct node
{
KEY_T id;
DATA_T data;
};

and you only compare the key, not *all* the members of the struct.

When a programmer name a function cmp(), I expect a compare operation to
be defined. If only testing for equality, a far better name choice would
be e.g. 'is_equal()'.

See return value of memcmp, strcmp, ... it's <0, 0 or >0.

--
Tor <torust [at] online [dot] no>
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      09-16-2007
pete wrote:
> Tor Rustad wrote:
>
>> Furthermore, if writing a cmp() function, I rather prefer to use the
>> same interface and return value as qsort() and bsearch() expect:
>>
>> int cmp_mystruct(const void *, const void *)
>>
>> why compare "struct's", unless doing sorting and searching?

>
> If you had a linked list of structures
> and if you had a list sorting function that
> took a compar function pointer as an argument,
> then it would make more sense to have pointers to the list node type.


Agreed, in this case, I wouldn't use void*, but rather

int is_equal(KEY_T, KEY_T );
int is_less (KEY_T, KEY_T );

--
Tor <torust [at] online [dot] no>
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      09-16-2007
Richard Heathfield wrote:
> CBFalconer said:


[...]

>> The definition of cmp is up to you.

>
> Precisely.


The compiler doesn't mind bad identifiers, but humans do...

The point is that the standard C library, the *is* functions return
differently, than the *cmp* functions do.


So, if you are consistent, you can't object to code with:

int is_zero(int c)
{
return ('0' != c);
}

either.

--
Tor <torust [at] online [dot] no>
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-16-2007
Tor Rustad said:

> Richard Heathfield wrote:
>> CBFalconer said:

>
> [...]
>
>>> The definition of cmp is up to you.

>>
>> Precisely.

>
> The compiler doesn't mind bad identifiers, but humans do...
>
> The point is that the standard C library, the *is* functions return
> differently, than the *cmp* functions do.
>
>
> So, if you are consistent, you can't object to code with:
>
> int is_zero(int c)
> {
> return ('0' != c);
> }
>
> either.
>



You lost me. Remember that my preference is for relational comparison, a
la strcmp and memcmp, rather than for equality-only comparison.

If you realised this and still think you have a point, I'd be curious to
know what that point is.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Spacing and timing for comparing algorithms and data-structures Alec Taylor Python 0 03-02-2012 04:55 AM
comparing sizes of two structures subramanian100in@yahoo.com, India C Programming 20 03-08-2007 09:20 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM
SystemStackError comparing self-referential structures Joel VanderWerf Ruby 2 08-22-2003 01:14 PM



Advertisments