If I have:
struct one_{
unsigned int one_1;
unsigned short one_2;
unsigned short one_3;
};
struct two_{
unsigned int two_1;
unsigned short two_2;
unsigned char two_3;
};
struct mystruct{
struct one_ one;
struct two_ two;
}mystruct1;
Then could I by any change ask on the value of the whole struct mystruct1,
that is all the elements in the struct in one call? I want to do something
like (in pseudo like language):
if(mystruct1 == 0) { print("All elements of mystruct1 is zero");}
Best Regards
Terry 258 8728
Terry Andersen wrote: If I have:
struct one_{ unsigned int one_1; unsigned short one_2; unsigned short one_3; };
struct two_{ unsigned int two_1; unsigned short two_2; unsigned char two_3; };
struct mystruct{ struct one_ one; struct two_ two; }mystruct1;
Then could I by any change ask on the value of the whole struct mystruct1, that is all the elements in the struct in one call? I want to do something like (in pseudo like language):
if(mystruct1 == 0) { print("All elements of mystruct1 is zero");}
No.
You could, however, do this:
int CompareMyStruct sForEquality(co nst struct mystruct *ms1,
const struct mystruct *ms2)
{
int same = 0;
if(ms1->one.one_1 == ms2->one.one_1 &&
ms1->one.one_2 == ms2->one.one_2 &&
ms1->one.one_3 == ms2->one.one_3 &&
ms1->two.one_1 == ms2->two.one_1 &&
ms1->two.one_2 == ms2->two.one_2 &&
ms1->two.one_3 == ms2->two.one_3)
{
same = 1;
}
return same;
}
You can then do:
struct mystruct z = {0};
if(CompareMyStr uctsForEquality (&z, &mystruct1) == 1)
{
puts("mystruct1 is zeroed.");
}
--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
On 2003-10-25, Richard Heathfield <do******@addre ss.co.uk.invali d> wrote: Terry Andersen wrote:
struct one_{unsigned int one_1;unsigned short one_2;unsigned short one_3;}; struct two_{unsigned int two_1;unsigned short two_2;unsigned char two_3;}; struct mystruct{struct one_ one;struct two_ two;}mystruct1;
Then could I by any change ask on the value of the whole struct mystruct1, that is all the elements in the struct in one call? I want to do something like (in pseudo like language):
if(mystruct1 == 0) { print("All elements of mystruct1 is zero");}
No.
[good stuff snipped]
If you knew for sure that struct mystruct did not have any padding bits
(and you can not portably determine this), or if you knew that struct
mystruct is always initialized in a way such that all its padding bits
are zero (such as allocated by calloc(), or was initialized by a call to
memset(p, '\0', sz)), you could have used the function memcmp().
{
static const struct mystruct z;
if(memcmp(&myst ruct1, &z)) { print("All elements of mystruct1 is zero"); }
}
But, Richard's suggestion is the most straight forward and less
error prone.
-- James
James Hu wrote:
<snip> If you knew for sure that struct mystruct did not have any padding bits (and you can not portably determine this), or if you knew that struct mystruct is always initialized in a way such that all its padding bits are zero (such as allocated by calloc(), or was initialized by a call to memset(p, '\0', sz)), you could have used the function memcmp().
{ static const struct mystruct z; if(memcmp(&myst ruct1, &z)) { print("All elements of mystruct1 is zero");
I think you'll need sizeof z as a third argument. :-)
--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
"Richard Heathfield" <do******@addre ss.co.uk.invali d> wrote in message
news:bn******** **@hercules.bti nternet.com... You can then do:
struct mystruct z = {0}; if(CompareMyStr uctsForEquality (&z, &mystruct1) == 1) { puts("mystruct1 is zeroed."); }
Aha, I've found a good use for immediate structs.
if(CompareMyStr uctsForEquality (&z, &(struct mystruct){0}) == 1)
.....
James Hu wrote: Terry Andersen wrote: struct one_{unsigned int one_1;unsigned short one_2;unsigned short one_3;}; struct two_{unsigned int two_1;unsigned short two_2;unsigned char two_3;}; struct mystruct{struct one_ one;struct two_ two;}mystruct1;
If you knew for sure that struct mystruct did not have any padding bits (and you can not portably determine this)
Why can't you portably determine this? In particular, what's to stop
you from calculating the number of bits in each integer type (using
sizeof and CHAR_BIT) and then comparing the maximum value that can be
represented with that numbers of bits to the actual maximum value for
the type?
Padding /bytes/ in the structure can be detected by comparing the size
of the structure with the sum of the sizes of its members, of course.
Jeremy.
I'm not familiar with this {0} syntax. This creates a struct of the correct
type where all the members have value 0? Is this C99 or something?
"Serve Lau" <i@bleat.nospam .com> wrote in message
news:bn******** **@news2.tilbu1 .nb.home.nl... "Richard Heathfield" <do******@addre ss.co.uk.invali d> wrote in message news:bn******** **@hercules.bti nternet.com... You can then do:
struct mystruct z = {0}; if(CompareMyStr uctsForEquality (&z, &mystruct1) == 1) { puts("mystruct1 is zeroed."); }
Aha, I've found a good use for immediate structs.
if(CompareMyStr uctsForEquality (&z, &(struct mystruct){0}) == 1) ....
[Please don't top-post. It's very irritating. Fixed.]
Roose wrote: "Serve Lau" <i@bleat.nospam .com> wrote in message news:bn******** **@news2.tilbu1 .nb.home.nl... "Richard Heathfield" <do******@addre ss.co.uk.invali d> wrote in message news:bn******** **@hercules.bti nternet.com... > You can then do: > > struct mystruct z = {0}; > if(CompareMyStr uctsForEquality (&z, &mystruct1) == 1) > { > puts("mystruct1 is zeroed."); > }
Aha, I've found a good use for immediate structs.
if(CompareMyStr uctsForEquality (&z, &(struct mystruct){0}) == 1) .... I'm not familiar with this {0} syntax. This creates a struct of the correct type where all the members have value 0? Is this C99 or something?
The syntax struct mystruct z = {0}; is straight C90 - you can use it now,
today. When you partially initialise an aggregate (such as an array or
struct) with *at least* one element, it is the compiler's job to initialise
everything else to static defaults, which are basically 0, 0.0 and NULL.
The construct &(struct mystruct){0}, which uses a "compound literal", is
C99-only.
--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
"Roose" <no****@nospam. nospam> wrote in message
news:HA******** *********@newss vr27.news.prodi gy.com... I'm not familiar with this {0} syntax. This creates a struct of the
correct type where all the members have value 0? Is this C99 or something?
yes, although named struct also worked in C89
struct mystruct x = {0};
x gets initialized according to the initializer list. If x has more members
than there are items in the initializer list they will be filled with 0. So
x = {0} effectively fills the struct with 0 and my code example does it with
a nameless struct.
compared to memset(&x, 0 sizeof x); this is the most portable construct,
least to type and a compiler can optimize the most out of it. So why not use
it? :)
Thank you for your answer, that cleared it up nicely.
Please don't bottom-post, I find it irritating since when reading a thread,
I basically scroll through/read O(n^2) posts rather than O(n). I find it
_extremely_ irritating when people complain about top-posting, as if they
were the President of UseNet. Even after a decade or more reading it.
"Richard Heathfield" <do******@addre ss.co.uk.invali d> wrote in message
news:bn******** **@titan.btinte rnet.com... [Please don't top-post. It's very irritating. Fixed.]
The syntax struct mystruct z = {0}; is straight C90 - you can use it now, today. When you partially initialise an aggregate (such as an array or struct) with *at least* one element, it is the compiler's job to
initialise everything else to static defaults, which are basically 0, 0.0 and NULL.
The construct &(struct mystruct){0}, which uses a "compound literal", is C99-only.
-- Richard Heathfield : bi****@eton.pow ernet.co.uk "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton This thread has been closed and replies have been disabled. Please start a new discussion. |