Mesg posted by "Paul Herring" <[EMAIL PROTECTED]>,deleted accedently, Sorry for
the inconvenience, Moderator.
From: guru_bo [mailto:[EMAIL PROTECTED]
>Question is
>#include<stdio.h>
>#include<conio.h>
>
>void main()
>{
>
> struct BIT
> {
> int op1:5, op2:12;
> char d1;
> int reg;
> }Bitfields;
>
> clrscr();
>
>printf("%d is the size of the structure",sizeof(Bitfields));
>
>}
>
>What is the output
>
>Ans is 6,
>My reasoning is
>
>Assuming int is 2 byte
You say that...
>op1:5 - 1, nearest byte is 5 + 3 .. 1byte
>op2:12 - 2 bytes
>char d1 - 1 byte
>int reg - 1 byte
Then claim int reg only takes 1 byte? It's more than likely 2.
>is it correct
With the exception of the probable typo, probably. You can get more
information out of that struct:
#include <stdio.h>
#include <stddef.h>
#define poffset(a,b) printf(" %s: %d\n", #b, offsetof(a,b));
typedef struct BIT{
int op1:5, op2:12;
char d1;
int reg;
}Bitfields;
int main(void){
poffset(Bitfields, d1);
poffset(Bitfields, reg);
printf("sizeof(Bitfields)=%d\n",sizeof(Bitfields));
return 0;
}
$ testc
d1: 4
reg: 8
sizeof(Bitfields)=12
$
That's with padding. If I turn padding off (aka 1 byte alignment):
$ testc
d1: 4
reg: 5
sizeof(Bitfields)=9
$
--
PJH
"Real programmers can write assembly code in any language." - Larry Wall
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire,
GL12 8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272
Website : www.alderley.com Sales : [EMAIL PROTECTED] Service : [EMAIL
PROTECTED]
This email and its contents are confidential and are solely for the use of
the intended
recipient. If you are not the original recipient you have received it in
error and any
use, dissemination, forwarding, printing or copying of this email is
strictly prohibited.
Should you receive this email in error please immediately notify [EMAIL
PROTECTED]
This email has been scanned for viruses, however you should always scan
emails with your
own systems prior to opening.
------------------------ Yahoo! Groups Sponsor --------------------~-->
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~->
To unsubscribe : [EMAIL PROTECTED]
Yahoo! Groups Links
<*> To reply to this message, go to:
http://groups.yahoo.com/group/Programmers-Town/post?act=reply&messageNum=4938
Please do not reply to this message via email. More information here:
http://help.yahoo.com/help/us/groups/messages/messages-23.html
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Programmers-Town/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/