Re: [SLUG] c programming - structures

2004-10-18 Thread Robert Collins
On Mon, 2004-10-18 at 11:21 +1000, torquemada wrote:
 understanding structures and a knowledge beforehand of what
 each element is, is essential. dereferencing any pointers
 might be needed to determine if the structures are identical or not.
 
 refer to the data-dictionary for the structure or documentation in that
 case.
 
 one could even hypothetically have a pointer embedded in a structure
 that points back to the original structure and one might end up in a
 infinite loop when comparing. but that would
 be an extreme case. take care and have fun.

Thats not particularly extreme or hypothetical. A circular buffer for
example, implemented as a list of buffers, is an immediate example. So
are any trees with uplinks.

Rob

-- 
GPG key available at: http://www.robertcollins.net/keys.txt.


signature.asc
Description: This is a digitally signed message part
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Re: [SLUG] c programming - structures

2004-10-17 Thread torquemada

when comparing structures, compare each member.
maybe read some good books on c programming too which
will explain how c programmers use structures. kr comes to mind

using a memcmp or similar as a rule of thumb is useful but
not very much as it won't dereference any elements that are pointers.
so compare each element and member.

cordially
Torquemada

On Sat, 16 Oct 2004, Lucas King wrote:

 hello,

 how does one compare two structures of the same type?

 Anjuta is returning an error when i compile :
   if (sin1 == sin2) {
 ..
 ..
   }

 where sin1 and sin2 are structures of the same type.  the sin structure
 is sockaddr_in.

 thanking you in advance,

 Lucas

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] c programming - structures

2004-10-17 Thread Benno
On Mon Oct 18, 2004 at 11:02:46 +1000, torquemada wrote:

when comparing structures, compare each member.
maybe read some good books on c programming too which
will explain how c programmers use structures. kr comes to mind

using a memcmp or similar as a rule of thumb is useful but
not very much as it won't dereference any elements that are pointers.
so compare each element and member.

Of course it is totally domain specific as to whether it is correct
to compare the pointer itself, or the value pointed to. (Which
of course could be another struct!)

Benno
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] c programming - structures

2004-10-17 Thread torquemada

understanding structures and a knowledge beforehand of what
each element is, is essential. dereferencing any pointers
might be needed to determine if the structures are identical or not.

refer to the data-dictionary for the structure or documentation in that
case.

one could even hypothetically have a pointer embedded in a structure
that points back to the original structure and one might end up in a
infinite loop when comparing. but that would
be an extreme case. take care and have fun.

cordially
Torquemada

On Mon, 18 Oct 2004, Benno wrote:

 On Mon Oct 18, 2004 at 11:02:46 +1000, torquemada wrote:
 
 when comparing structures, compare each member.
 maybe read some good books on c programming too which
 will explain how c programmers use structures. kr comes to mind
 
 using a memcmp or similar as a rule of thumb is useful but
 not very much as it won't dereference any elements that are pointers.
 so compare each element and member.

 Of course it is totally domain specific as to whether it is correct
 to compare the pointer itself, or the value pointed to. (Which
 of course could be another struct!)

 Benno
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


[SLUG] c programming - structures

2004-10-16 Thread Lucas King
hello,
how does one compare two structures of the same type?
Anjuta is returning an error when i compile :
 if (sin1 == sin2) {
   ..
   ..
 }
where sin1 and sin2 are structures of the same type.  the sin structure 
is sockaddr_in.

thanking you in advance,
Lucas


The information contained in this e-mail message and any accompanying files
is or may be confidential.If you are not the intended recipient, any use, 
dissemination, reliance,forwarding, printing or copying of this e-mail or
any attached files is unauthorised.This e-mail is subject to copyright. No
part of it should be reproduced,adapted or communicated without the written
consent of the copyright owner.If you have received this e-mail in error,
please advise the sender immediately by return e-mail, or telephone and
delete all copies.Fairfax does not guarantee the accuracy or completeness
of any information contained in this e-mail or attached files. Internet
communications are not secure, therefore Fairfax does not accept legal
responsibility for the contents of this message or attached files.


--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] c programming - structures

2004-10-16 Thread Benno
On Sat Oct 16, 2004 at 16:21:04 +1000, Lucas King wrote:
hello,

how does one compare two structures of the same type?

Anjuta is returning an error when i compile :
 if (sin1 == sin2) {
   ..
   ..
 }

where sin1 and sin2 are structures of the same type.  the sin structure 
is sockaddr_in.

thanking you in advance,


You need to either compare each element, or use memcp; eg:

if (memcmp(sin1, sin2, sizeof sin1) == 0) {


}

Benno
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] c programming - structures

2004-10-16 Thread Jan Schmidt
quote who=Benno
 On Sat Oct 16, 2004 at 16:21:04 +1000, Lucas King wrote:
 how does one compare two structures of the same type?
 You need to either compare each element, or use memcp; eg:
 
 if (memcmp(sin1, sin2, sizeof sin1) == 0) {
 
 
 }

and memcmp isn't usually a great idea, because most structures have padding
bytes that will be uninitialised (random) data.

J.
-- 
Jan Schmidt  [EMAIL PROTECTED]

I came for the quality. I stayed for the freedom. -- Sean Neakums
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] c programming - structures

2004-10-16 Thread amos
Jan Schmidt wrote:
quote who=Benno
On Sat Oct 16, 2004 at 16:21:04 +1000, Lucas King wrote:
how does one compare two structures of the same type?
You need to either compare each element, or use memcp; eg:
if (memcmp(sin1, sin2, sizeof sin1) == 0) {
}

and memcmp isn't usually a great idea, because most structures have padding
bytes that will be uninitialised (random) data.
J.
You can overcome this by memset(3)'ing the struc to zero's when it's 
allocated, or use calloc(3).

How usually do you compare structures? Member by member?
C++'s default comparison is at the bit level, how does it overcome
the uninitialized padding problem you pointed?
Cheers,
--Amos
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html