473,396 Members | 1,836 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,396 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 1889
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: 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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.