Connecting Tech Pros Worldwide Forums | Help | Site Map

Comapring structs

Yong Jing
Guest
 
Posts: n/a
#1: Sep 6 '06
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)


dcorbit@connx.com
Guest
 
Posts: n/a
#2: Sep 6 '06

re: Comapring structs


Yong Jing wrote:
Quote:
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?
Quote:
>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.
Quote:
Regards,
Yong jin (~.~E)
Sjouke Burry
Guest
 
Posts: n/a
#3: Sep 6 '06

re: Comapring structs


Yong Jing wrote:
Quote:
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]))
Robert Gamble
Guest
 
Posts: n/a
#4: Sep 6 '06

re: Comapring structs


Sjouke Burry wrote:
Quote:
Yong Jing wrote:
Quote:
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

Keith Thompson
Guest
 
Posts: n/a
#5: Sep 6 '06

re: Comapring structs


Sjouke Burry <burrynulnulfour@ppllaanneett.nnlllwrites:
Quote:
Yong Jing wrote:
Quote:
>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) kst-u@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.
dcorbit@connx.com
Guest
 
Posts: n/a
#6: Sep 6 '06

re: Comapring structs


Sjouke Burry wrote:
Quote:
Yong Jing wrote:
Quote:
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).

Closed Thread