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
What is the purpose of macro in C language?
What is function definition in c?
What is equivalent to ++i+++j?
int i=3; this declaration tells the C compiler to a) reserve space in memory to hold the integer value b) associate the name i with this memory location c) store the value 3 at this location d) all the above
How are pointers declared in c?
What is difference between structure and union with example?
What are lookup tables in c?
Does sprintf put null character?
What is graph in c?
What is clrscr in c?
#include main() { char s[] = "Bouquets and Brickbats"; printf(" %c, ",*(&s[2])); printf("%s, ",s+5); printf(" %s",s); printf(" %c",*(s+2)); }
What is the purpose of 'register' keyword?
What are identifiers and keywords in c?
Define macros.
the portion of a computer program within which the definition of the variable remains unchanged a) mode b) module c) scope d) none