www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/08/18/21:17:19

From: fredex AT fcshome DOT stoneham DOT ma DOT us
Message-Id: <199608190058.UAA00270@fcshome.stoneham.ma.us>
Subject: Re: What am i doing wrong?
To: hubble AT hem DOT passagen DOT se (Andreas Vernersson)
Date: Sun, 18 Aug 1996 20:58:31 -0400 (EDT)
Cc: djgpp AT delorie DOT com
In-Reply-To: <199608181702.AA28727@mail.umu.se> from "Andreas Vernersson" at Aug 18, 96 07:02:20 pm

Thinking furiously, Andreas Vernersson wrote:
> 
> Could anyone explain this?

Sure.

> typedef struct a
> {	unsigned int q2 __attribute__((packed));
> 	unsigned char qq __attribute__((packed));
> } b;
> typedef struct a2
> {	
> 	unsigned int q2;
> 	unsigned char qq;
> } b2;
> void main()
> {
> 	printf("size: %d\n",(int)sizeof(b));  // 5 ???
> 	printf("size: %d\n",(int)sizeof(b2));  // 8  ???
> }
> I only wants the datastructure to be 2+1=3 bytes... 

An int *is* four bytes. 4 + 1 = 5.
Try doing:

	printf ("%d\n", sizeof(int));

and see what it prints.

If you're expecting it to be 3  merely because you believe an int ought
to be (or is) 2 bytes, then you're going to be disappointed.

OTOH, if you REQUIRE that it be 3 bytes (for example if it has to 
match a register that's that size on a piece of hardware), then you
need to use a 16-bit intergral type, such as short or unsigned short.

Fred
-- 
-------------------------------------------------------------------------------
 .----    Fred Smith    /                        Office: fred AT computrition DOT com 
( /__  ,__.   __   __ /  __   : /                                 508-663-2524 
 /    /  /   /__) /  /  /__) .+'           Home: fredex AT fcshome DOT stoneham DOT ma DOT us 
/    /  (__ (___ (__(_ (___ / :__                                 617-438-5471 
-------------------------------- Jude 1:24,25 ---------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019