Compare record values without assuming their byte layout

For Packet, explain why neither a==b nor a guessed sizeof(Packet)==5 is a portable way to answer the programmer’s questions. Write a field-based equality function and give the exact output of the supplied correct program. Draw a possible layout symbolically using T=sizeof(char), I=sizeof(int), internal padding P and trailing padding Q; do not assert numerical P or Q. Explain why comparing all object bytes with memcmp is not a general substitute for comparing logical field values, even when both records’ fields are initialized.

#include <stdio.h>
typedef struct {
    char tag;
    int count;
} Packet;
static int same_packet(Packet a, Packet b) {
    return a.tag == b.tag && a.count == b.count;
}
int main(void) {
    Packet a = {'A', 2};
    Packet b = a;
    printf("equal=%d\n", same_packet(a, b));
    b.count = 3;
    printf("equal=%d\n", same_packet(a, b));
    printf("enough_storage=%d\n", sizeof(Packet) >= sizeof a.tag + sizeof a.count);
    return 0;
}

