In below code. When I print individual char array size it prints correct value. But when I print whole struct size at a time it adds one byte more for each char array.
How can I control padding between elements in struct using #pragma in GNU C++ compiler? Can I set it to 0?
#include <iostream>
using namespace std;
typedef struct s1{
char test[15];
char t2[15];
}s1;
typedef struct s2{
int message_id;
int message_size;
}s2;
typedef struct s3{
char test[15];
char t2[15];
int a;
}s3;
typedef struct s4{
char test[15];
int a;
}s4;
int main() {
s4 st;
cout<<sizeof(s1)<<endl;
cout<<sizeof(s2)<<endl;
cout<<sizeof(s3)<<endl;
cout<<sizeof(s4)<<endl;
cout<<sizeof(st.test)<<endl;
return 0;
}
-----------------------OUTPUT--------------------
30
8
36
20
15
How can I control padding between struct elements?