473,659 Members | 2,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1908
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.comwrit es:
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_Keit h) 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
6084
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 uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an event called NETWORK_TIMEOUT) and more complex events that contain data (for example an event called...
4
11444
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 parser myself. Just wanted to see if anyone could provide me with some constructive criticism :) any feedback would be greatly appreciated ----------------------------------------------------------------------------- #include "stdio.h" #include...
13
5725
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 hoping the clc crowd had some ideas for making it even simpler! In-lined below the full program (132 lines). It's a circular singular linked list of "cells" inspired by the one in Plauger's text
13
4113
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 dynamic array of characters int length; //number of characters } String;
6
3785
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 trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple coding convention for reading in a flatfile - one record at a time?? SCANF does not work because of...
8
5105
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
2
5180
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 sends the message "Ping" to the server, which reads it and answers with a "Pong". The game is really simple and the coding should be also very simple! But for me it isn't. By the way, the program uses datagram sockets (UDP). And, I'm using
13
3153
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 stored in an object is read from another object that overlaps in any way the storage of the first object, then the overlap shall be exact and the two objects shall
4
2105
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 raw C will give me more experience. I wrote a simple lexer ( changes leksems into the streams of tokens). The Lisp dialect I have to implement is not defined in any formal way, but I have a description of how it should behave (say its a >>writen...
17
5807
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: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
8747
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.