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
Can you assign a different address to an array tag?
What is a structure in c language. how to initialise a structure in c?
Calculate 1*2*3*____*n using recursive function??
What is self-referential structure in c programming?
what are the advantages of a macro over a function?
When should volatile modifier be used?
which of the following statement is wrong a) mes=123.56; b) con='T'*'A'; c) this='T'*20; d) 3+a=b;
what is recursion in C
What is a null string in c?
What are the characteristics of arrays in c?
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.
Differentiate between ordinary variable and pointer in c.
How do I use void main?
Do array subscripts always start with zero?
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?