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 you assign a different address to an array tag?

704


What is a structure in c language. how to initialise a structure in c?

613


Calculate 1*2*3*____*n using recursive function??

1522


What is self-referential structure in c programming?

662


what are the advantages of a macro over a function?

650






When should volatile modifier be used?

558


which of the following statement is wrong a) mes=123.56; b) con='T'*'A'; c) this='T'*20; d) 3+a=b;

2419


what is recursion in C

620


What is a null string in c?

593


What are the characteristics of arrays in c?

618


write a program to reverse a every alternetive words in a string in a place. EX: Input is "this is the line of text" Output should be "shit is eht line fo text" Please any one tell me code for that.

1580


Differentiate between ordinary variable and pointer in c.

625


How do I use void main?

637


Do array subscripts always start with zero?

788


How to write a program for machine which is connected with server for that server automatically wants to catch the time for user of that machine?

1596