473,770 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Asking if elements in struct arre zero

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
Nov 13 '05
258 8747
Roose wrote:
Thank you for your answer, that cleared it up nicely.
No problem.
Please don't bottom-post, I find it irritating since when reading a
thread,
You like reading upside-down?
I basically scroll through/read O(n^2) posts rather than O(n).


Then encourage people to snip properly, and snip properly yourself to set an
example.

FCOL.

--
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
Nov 13 '05 #11
On 2003-10-26, Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> wrote:
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?


I had a misthought. Yes, you can portably determine this, but it would
not be a strictly conforming program.

Thanks,

-- James
Nov 13 '05 #12
On 2003-10-25, Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
James Hu wrote:
{
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. :-)


Yep!

Thanks,

-- James
Nov 13 '05 #13
On Sun, 26 Oct 2003 20:35:18 GMT
"Roose" <no****@nospam. nospam> wrote:
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.


<snip>

Bottom posting is the long established convention on Usenet, although on
*some* non-technical groups top posting is permitted. It is also
enshrined, together with snipping, in one of the RFCs.

You are also likely to quickly find yourself ignored by a number of the
more knowledgeable posters here if you persist in top posting.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #14
Roose wrote:

[8<]
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

[>8]

Bottom-posting is the established norm in this forum. Proposals
to change that norm have been made from time to time; but there
has been little interest in adopting such proposals.

You can, of course, choose not to read bottom-posted articles;
just as you can choose to top-post your replies. I suspect that
such deviant behavior might not be well-regarded - and that you
might well find yourself standing all alone in the crowd.

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall very far from the tree.

Nov 13 '05 #15
On Sun, 26 Oct 2003 20:35:18 GMT, in comp.lang.c , "Roose"
<no****@nospam. nospam> wrote:
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).
only if idiots don't Snip the irrelevant material .Sheesh, what is it
with some people? Too lazy to snip?
I find it
_extremely_ irritating when people complain about top-posting,
Yeah? Well round here, top posting is frowned on, so stop it.
as if they
were the President of UseNet. Even after a decade or more reading it.


you should know better.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #16
OK, feel free to ignore my posts. Like I said, after a decade of posting in
UseNet, I have never had a problem getting any answer to any question, with
top-posting preferred. I do bottom-post when there are multiple points to
address, but in general I find it to read when people top-post (and snip).
If you have a newsreader made after 1991, it is no problem to track the
thread backward.

Why can't we all be grown-ups and accept that there are things in this big
world that we can't control. I prefer top-posting, but I have never
complained about anyone bottom-posting, and certainly would never go to
absurd lengths like actually rearranging the posts.

I also find the all the sigs with the stupid quotes and URLs rather
annoying, but I've never complained about it.

Nov 13 '05 #17
[regarding]
if(CompareMyStr uctsForEquality (&z, &(struct mystruct){0}) == 1)

In article <bn**********@t itan.btinternet .com>
Richard Heathfield <bi****@eton.po wernet.co.uk> writes, in part:The construct &(struct mystruct){0}, which uses a "compound literal", is
C99-only.


Yes. In this particular case -- comparing, not writing on, the
structure -- you might want to use:

&(const struct mystruct){0}

i.e., add a "const" qualifier, provided the compare() function takes
const-qualified pointers. This not only makes the structure contents
read-only (at least in principle), but also informs the compiler that
it is allowed to share storage with other such "const struct mystruct"s.

In other words, barring particularly tricky optimization, the sequence:

extern void foo(const struct S *, const struct S *);

foo(&(struct S){0}, &(struct S){0});

*must* pass two *different* pointer values to function foo(), while
the call:

foo(&(const struct S){0}, &(const struct S){0});

is allowed (but not required) to pass identical pointer values to
foo(). If foo() consists of:

void foo(const struct S *a, const struct S *b) {
printf("in foo(): have %s pointers\n",
a == b ? "same" : "different" );
}

then the difference is visible and you can tell whether your C99
compiler implements the "share readonly compound-literal storage"
optimization.

(Disclaimer: this is all based on a C99 draft, and this sort of
fiddly stuff about precisely when and where storage is allocated is
the kind of thing that changes from draft to draft. :-) )
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #18
Roose wrote:
OK, feel free to ignore my posts.


Verywell. Top posting is increddibly irritating.

*plonk*

--
Noah Roberts
- "If you are not outraged, you are not paying attention."

Nov 13 '05 #19
Roose wrote:

OK, feel free to ignore my posts.

You got it.
*plonk*


Brian Rodenborn
Nov 13 '05 #20

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

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.