By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

simple struct

P: n/a
i have some an array of structs, i need to compare arrays. what can you
do me for ?thanx
Potatoman

Jul 12 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Potatoman said:
i have some an array of structs, i need to compare arrays. what can you
do me for ?
Please ask your question again, this time providing all the necessary
information that will enable us to give you an answer.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 12 '06 #2

P: n/a

Ok, no problem! (*o*)

typedef struct{int a;int b}s;
s s[5];

///Initialize all 5's values of a and b
//compare s[0],s[1],s[2],s[3],s[4] <===== I would like to know this

Could you help me ?

Jul 12 '06 #3

P: n/a
Potatoman wrote:
i have some an array of structs, i need to compare arrays. what can you
do me for ?thanx
I can review your code when you post it. I doubt that anyone is going to
bother doing your work for you if you don't attempt it first.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 12 '06 #4

P: n/a

Flash Gordon のメッセージ:
Potatoman wrote:
i have some an array of structs, i need to compare arrays. what can you
do me for ?thanx

I can review your code when you post it. I doubt that anyone is going to
bother doing your work for you if you don't attempt it first.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
no, please, you don:t have to do the work for me, just give me hints to
how i can compare the 2 structs. When the number of struct's elements
gets larger, comparing each of them is clearly incorrect. please tell
something

Jul 12 '06 #5

P: n/a
Potatoman said:
>
Ok, no problem! (*o*)

typedef struct{int a;int b}s;
s s[5];

///Initialize all 5's values of a and b
//compare s[0],s[1],s[2],s[3],s[4] <===== I would like to know this

Could you help me ?
Sure. Here's a way to compare two structs of type s, in such a way that
qsort will understand the comparison:

int cmp_s(const void *vp1, const void *vp2)
{
const s *p1 = vp1;
const s *p2 = vp2;
int diff = (p1->a p2->a) - (p1->a < p2->a);
if(0 == diff)
{
diff = (p1->b p2->b) - (p1->b < p2->b);
}
return diff;
}

You can now sort your array, passing cmp_s as the fourth parameter to qsort.
The lowest-value struct will now be in element 0 of your array, the
next-lowest in element 1, etc.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 12 '06 #6

P: n/a
Potatoman said:

<snip>
When the number of struct's elements
gets larger, comparing each of them is clearly incorrect.
As the number of members in the struct grows during development, so does the
probability that a subset of those members can logically be grouped into a
separate struct which can then become a separate member in its own right,
with its own comparison function.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 12 '06 #7

P: n/a
"Potatoman" <gu*************@yahoo.comwrites:
Flash Gordon のメッセージ:
>Potatoman wrote:
i have some an array of structs, i need to compare arrays. what can you
do me for ?thanx

I can review your code when you post it. I doubt that anyone is going to
bother doing your work for you if you don't attempt it first.

no, please, you don:t have to do the work for me, just give me hints to
how i can compare the 2 structs. When the number of struct's elements
gets larger, comparing each of them is clearly incorrect. please tell
something
What exactly do you mean by "compare"?

If you want to test whether the corresponding members of two struct
objects are equal, comparing each member is really the only portable
way to do it. (You might be tempted to use memcmp(), but this can
fail for a number of reasons, notably the presence of padding between
members.)

In your original question, you talked about an array of structs. Now
you're asking about comparing two structs.

If you can't clearly state the problem, you're not going to be able to
solve it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 12 '06 #8

P: n/a
Potatoman wrote:
no, please, you don:t have to do the work for me, just give me hints to
how i can compare the 2 structs. When the number of struct's elements
gets larger, comparing each of them is clearly incorrect.
It's not "clearly incorrect". It may or may not be correct, depending
on what you mean by "compare" and what you mean by "comparing each of
them". You haven't said what either of those mean to you.

--
Chris "seeker" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Jul 12 '06 #9

P: n/a

Chris Dollin のメッセージ:
Potatoman wrote:
no, please, you don:t have to do the work for me, just give me hints to
how i can compare the 2 structs. When the number of struct's elements
gets larger, comparing each of them is clearly incorrect.

It's not "clearly incorrect". It may or may not be correct, depending
on what you mean by "compare" and what you mean by "comparing each of
them". You haven't said what either of those mean to you.

--
Chris "seeker" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/
Oh, thanks everyone, please tell me all about all types of struct
comaprison

Jul 12 '06 #10

P: n/a
Potatoman wrote:
Chris Dollin のメッセージ:
>Potatoman wrote:
no, please, you don:t have to do the work for me, just give me hints to
how i can compare the 2 structs. When the number of struct's elements
gets larger, comparing each of them is clearly incorrect.

It's not "clearly incorrect". It may or may not be correct, depending
on what you mean by "compare" and what you mean by "comparing each of
them". You haven't said what either of those mean to you.

Oh, thanks everyone, please tell me all about all types of struct
comaprison
No. There are too many. What we want to know is what /you/ want to
mean by a struct comparison. If you don't know, perhaps you'd better
go and brood on it.

You've shown us a struct of two ints. Surely you know what you want to
mean when you compare them -- what the results would be for comparing
among the structs I shall write as

(0, 0) (0, 1) (1, 0) (-1, 0) (0, -1) (-1, -1) (1, 1),
(-1, 1) (1, -1), (10, 9) (9, 10), (-1, 10), (32767, 0),
(0, 32767) ?

--
Chris "pairs, yum yum" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Jul 12 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.