You are using sizeof wrong. It expects a type to be passed to it, but you are passing an actual variable.
-
struct a p;
-
-
// Wrong
-
sizeof(p);
-
-
// Correct
-
sizeof(struct a);
-
No this is wrong sizeof can take a type or a variable and most people suggest using the variable name because then if the type changes the code stays correct with any further changes.
The problem lies in the way the structure is declared
-
struct {
-
int i; // 4 bytes
-
char c; // 1 byte
-
char b; // 1 byte
-
}a;
-
-
struct a p;
-
This does not define a type of structure named a, it defines a variable a that is a structure you need
-
struct a {
-
int i; // 4 bytes
-
char c; // 1 byte
-
char b; // 1 byte
-
};
-
-
struct a p;
-
This defines a type of structure named a which can then be used to define a variable p of that type.
P.S.
main()
should be
int main()
to avoid undefined behaviour