Similar questions were asked, but answers didn't specifically solve my confusion.
I have been playing around with C for few months, and I always thought that sizeof(AnyStruct) will be a multiple of four, until I came to this:
#include <stdio.h>
typedef struct
{
    int x;
    char y;
} S1;
typedef struct
{
    char x[4];
    char y;
} S2;
int main()
{
    printf("%d\n", (int)sizeof(S1));
    printf("%d\n", (int)sizeof(S2));
    return 0;
}
The output is
8
5
Any explanation on why alignment didn't work in S2 but worked in S1 ?
In general, when does it work and when it doesn't, and how it works.
 
     
    