Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How to avoid structure padding in C?

Answer Posted / lokesh mogra

yes u can use pragma to change change byte alignment.
for e.g.
typedef struct _s1{
unsigned int i;
unsigned char c;
unsigned long a;
unsigned short e;
} s1;
Size of this structure is of 11 Bytes. but due to default
byte alignment(8 byte) which is different for different
compilers. The size of structure would be 16 Bytes.
In order to change the alignment, we will have to do
something like this.

#pragma pack(push,1)
typedef struct _s1{
unsigned int i;
unsigned char c;
unsigned long a;
unsigned short e;
//unsigned char b;
} s1;
#pragma pack(pop)

This will change the byte alignment to 1 Byte. and thus size
of structure will be exactly 11 bytes

Is This Answer Correct ?    49 Yes 11 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we use any name in place of argv and argc as command line arguments?

1057


What is the difference between the local variable and global variable in c?

956


writ a program to compare using strcmp VIVA and viva with its output.

2002


What are reserved words?

1087


Explain how can you check to see whether a symbol is defined?

1149


What is static and volatile in c?

1209


What is the function of multilevel pointer in c?

1067


a function gets called when the function name is followed by a a) semicolon (;) b) period(.) c) ! d) none of the above

1374


what do you mean by inline function in C?

1044


What are the application of c?

1082


Which operators cannot be overloaded a) Sizeof b) .* c) :: d) all of the above

1128


How can I manipulate strings of multibyte characters?

1102


What is the scope of static variable in c?

997


Is c procedural or functional?

1005


explain what are pointers?

995