Re: problem in structure



RAKHE wrote:

hi

struct name
{
char n2;
short n1;
long n3;

};

here actually sizeof(struct name) is 7;
but why it is printing it as 8 in gcc

Because sizeof(struct name) _is_ 8.

if any one find the answer could you explain how it is happening

It's called "padding" and it's in the FAQ somewhere. I'm sure
someone will post the section shortly.

Basically, your system probably requires (or at least "prefers") to
have shorts on even byte boundaries (and, perhaps, longs on 4-byte
boundaries). Therefore, the compiler adds a byte of padding between
n2 and n1. Use the offsetof() macro to demonstrate this, by printing
the offset of n2, n1, and n3.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@xxxxxxxxx>


.



Relevant Pages

  • Re: Align numbers at decimal point
    ... yet any padding), as well as the space each occupies before the decimal ... You might make this part a bit more compact with a couple of clever list ... the 21st column of each row you're printing (meaning you know that no ... you need only a single loop and no extra memory...: ...
    (comp.lang.python)
  • Top Line of Field Missing In Report
    ... I have a problem with printing a report, which is the data from a single ... boundaries. ... I have Layout Set-up Views set to 'Form View'; the Layout Set-up Printing ...
    (comp.databases.filemaker)
  • Re: Text Box Lines
    ... The only way I know is to make sure that "Text boundaries" ... is selected on the View tab of Tools>Options. ... Stefan Blom ... as to where the tables are without printing the outline. ...
    (microsoft.public.word.docmanagement)
  • Re: Strange behaviour with printf
    ... but its also printing its copy type and status along with it. ... And, as structure members appear in memory in ... and you're lucky/unlucky that a terminator *is* being ... found - perhaps *because* of padding being added. ...
    (comp.lang.c)
  • Re:
and font sizes...
... I have a stylesheet like: ... Don't use pt - those are for printing. ... padding on the cells. ...
(alt.html)