473,241 Members | 1,489 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,241 software developers and data experts.

simple struct

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

Jul 12 '06 #1
10 1884
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

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

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
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
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
"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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
4
by: Greg B | last post by:
Well since getopt() doesn't seem to be compatible with Windows, and the free implementation of it for Windows that I found still had some annoying restrictions, I thought I'd whip up a simple...
13
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
2
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then...
13
by: Mike S | last post by:
I came across the following paragraph in the "Semantics" section for simple assignment in N1124 (C99 draft) and I'm wondering if I'm interpreting it right: 6.5.16.1p3: If the value being...
4
by: Thomas | last post by:
Hello, I am a CS student and I want to write simple lisp interpreter. The code should be entierly in C. I don't want to use any compiler generators like Bison or Yak, since wrinting this in...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.