473,670 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comapring structs

I have a struct of over 50 members

struct A {

type -- 50 members
A(){initialize all members}
}B[2];

How can I compare B[0] with B[1], please?

Regards,
Yong jin (~.~E)

Sep 6 '06 #1
5 1493
Yong Jing wrote:
I have a struct of over 50 members

struct A {

type -- 50 members
A(){initialize all members}
}B[2];

How can I compare B[0] with B[1], please?
>From the C FAQ:
2.8: Is there a way to compare structures automatically?

A: No. There is no single, good way for a compiler to implement
implicit structure comparison (i.e. to support the == operator
for structures) which is consistent with C's low-level flavor.
A simple byte-by-byte comparison could founder on random bits
present in unused "holes" in the structure (such padding is used
to keep the alignment of later fields correct; see question
2.12). A field-by-field comparison might require unacceptable
amounts of repetitive code for large structures.

If you need to compare two structures, you'll have to write your
own function to do so, field by field.

References: K&R2 Sec. 6.2 p. 129; Rationale Sec. 3.3.9; H&S
Sec. 5.6.2 p. 133.
Regards,
Yong jin (~.~E)
Sep 6 '06 #2
Yong Jing wrote:
I have a struct of over 50 members

struct A {

type -- 50 members
A(){initialize all members}
}B[2];

How can I compare B[0] with B[1], please?

Regards,
Yong jin (~.~E)
memcmp(&B[0],&B[1],sizeof(B[0]))
Sep 6 '06 #3
Sjouke Burry wrote:
Yong Jing wrote:
I have a struct of over 50 members

struct A {

type -- 50 members
A(){initialize all members}
}B[2];

How can I compare B[0] with B[1], please?

Regards,
Yong jin (~.~E)
memcmp(&B[0],&B[1],sizeof(B[0]))
That will tell you if the respresentation s of the two structures is
identical, including any padding, which is almost certainly not what
the OP is trying to accomplish. If memcmp returns 0 then you know the
two strutures are equal, otherwise you don't know anything, not
generally useful. This is because structures may contain padding which
does not contribute to the values of its members, this padding may be
different without affecting the values of the members. Even if you
knew that there was no padding, you wouldn't use memcmp to test for
equality for the same reason you wouldn't do this for any other type:
most types are allowed to have padding bits that don't contribute to
their value or multiple representations for the same values. If the
structures being compared contain any such types, they may not compare
equal with memcmp when their members have equivalent values. There is
a good reason that C does not allow the comparision of structures. The
solution is to write a function that compares the values of each member
of the structures.

Robert Gamble

Sep 6 '06 #4
Sjouke Burry <bu************ *@ppllaanneett. nnlllwrites:
Yong Jing wrote:
>I have a struct of over 50 members
struct A {
type -- 50 members
A(){initialize all members}
}B[2];
How can I compare B[0] with B[1], please?
Regards,
Yong jin (~.~E)
memcmp(&B[0],&B[1],sizeof(B[0]))
As the FAQ points out, this can cause errors if there are gaps between
members. Even if there are no gaps, it can potentially cause errors
of some members have types for which equality does not correspond to
bit-by-bit comparison (this can be the case, on some platforms, for
pointer and/or floating-point types).

Finally, it's not always clear that you *want* to compare all members.
Consider something like this:

struct dynamic_string {
size_t len;
char str[MAX_LEN];
};

If you want to compare two of these structures for *logical* equality,
you want to look at only the first "len" elements of the "str" member.

--
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.
Sep 6 '06 #5
Sjouke Burry wrote:
Yong Jing wrote:
I have a struct of over 50 members

struct A {

type -- 50 members
A(){initialize all members}
}B[2];

How can I compare B[0] with B[1], please?

Regards,
Yong jin (~.~E)
memcmp(&B[0],&B[1],sizeof(B[0]))
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct foo_t {
int id;
char *last_name;
char *first_name;
} foo_t;

int main(void)
{
char somefirst0[] = "Danniel";
char somefirst1[] = "Danniel";
char somelast0[] = "Corbit";
char somelast1[] = "Corbit";
foo_t foolist[2];
memset(foolist, sizeof foolist, 0);
foolist[0].id = 13;
foolist[1].id = 13;
foolist[0].last_name = somelast0;
foolist[1].last_name = somelast1;
foolist[0].first_name = somefirst0;
foolist[1].first_name = somefirst1;
if (0==memcmp(&foo list[0], &foolist[1], sizeof(foolist[0])))
printf("This foobird (%d/%s/%s) is equal to his brother
(%d/%s/%s).\n",
foolist[0].id,
foolist[0].last_name,
foolist[0].first_name,
foolist[1].id,
foolist[1].last_name,
foolist[1].first_name
);
else
printf("This foobird (%d/%s/%s) is NOT equal to his brother
(%d/%s/%s).\n",
foolist[0].id,
foolist[0].last_name,
foolist[0].first_name,
foolist[1].id,
foolist[1].last_name,
foolist[1].first_name
);
return 0;
}
C:\tmp>t
This foobird (13/Corbit/Danniel) is NOT equal to his brother
(13/Corbit/Danniel).

Sep 6 '06 #6

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

Similar topics

4
1997
by: news.microsoft.com | last post by:
Hi, I am using structs and am also using property accessors to access those private member fields... TO me this is a good way of handling them, but I find alot of people using direct access to the struct memebers, since structs is just a type similar to a class (with differences I know) why should I allow direct access like that... I looked at the Size and other parts on Control etc and I find its the same way by using property...
6
1700
by: James Pascoe | last post by:
Dear All, Apologies if this is OT. I have a C program which processes an arbitrary number of structs that are stored in a hash table. (The nature of the processing and the layout of the structs is irrelevant to this post, so I wont bore you with the details). My problem is that I need some way of distinguishing the processed
5
3123
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
10
2065
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required parameter. What would I need to do in order to convert a struct that looks like this: typedef struct { char rsvd0; char iadl1;
5
2910
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say that, a structure definition that includes function pointers only defines the function prototypes to be used with them, but not the actual implementations, whereas in C++, member functions cannot be changed *unless* virtual functions are used, or the
61
3740
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
14
1494
by: manstey | last post by:
Hi, Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1 and the difference is ('i','e') But if there was str1=yiqtol and str2=yaqtel, I am not interested.
11
2626
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out. This works without any problems. I now would like to filter "duplicate" records. They aren't really duplicate, I can't just qsort as is and eliminate the matching rows. I have number of fields that will be the same, but some of the fields will...
29
2763
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much overhead (I think). A Struct seems more appropriate. At least it is what I would have used in other languages. But since a Struct *can* hold methods, I wander if I am saving anything. If not, why use it?
43
3810
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a class, I always start by declaring the default constructor, copy constructor and assignment operator...
0
8471
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8815
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...
0
5686
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
4213
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...
1
2802
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
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.