473,513 Members | 2,559 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 1479
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 respresentations 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_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.
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(&foolist[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
1985
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...
6
1682
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...
5
3113
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
2055
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...
5
2890
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...
61
3691
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...
14
1482
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...
11
2605
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....
29
2742
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...
43
3771
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...
0
7260
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,...
0
7160
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...
1
7099
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...
0
5685
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 project—planning, coding, testing,...
1
5086
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...
0
4746
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...
0
1594
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 ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
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...

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.