simple struct

P

Potatoman

i have some an array of structs, i need to compare arrays. what can you
do me for ?thanx
Potatoman
 
R

Richard Heathfield

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.
 
F

Flash Gordon

Potatoman said:
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.
 
P

Potatoman

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 ?
 
P

Potatoman

Flash Gordon ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
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
 
R

Richard Heathfield

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.
 
R

Richard Heathfield

Potatoman said:

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.
 
K

Keith Thompson

Potatoman said:
Flash Gordon ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:


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.
 
C

Chris Dollin

Potatoman said:
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.
 
P

Potatoman

Chris Dollin ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
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
 
C

Chris Dollin

Potatoman said:
Chris Dollin ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:


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) ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top