Is struct variable a pointer?
No. Given the struct definition:
struct test
{
int a;
int b;
char c;
};
and the declaration:
struct test f;
you get the following in memory (assuming 4-byte ints):
+---+ ---+
f: | | |
+---+ |
| | |
+---+ +--- f.a
| | |
+---+ |
| | |
+---+ ---+
| | |
+---+ |
| | |
+---+ +--- f.b
| | |
+---+ |
| | |
+---+ ---+
| | f.c
+---+
Members are laid out in the order declared - f.a occupies the first four bytes of the object, f.b occupies the next four bytes, and f.c occupies the last byte. Additional "padding" bytes may be present to satisfy alignment requirements (in this case, the compiler will likely add 3 padding bytes after f.c so that the next object starts on an address that's a multiple of 4).
As you can see, the address of the first member of the struct (f.a) will be the same as the address of the entire struct object. There's no sort of padding or metadata before the first member.
To look at the various addresses and sizes, use the following:
printf( "address of f: %p\n", (void *) &f );
printf( "size of f: %zu\n", sizeof f );
printf( "address of f.a: %p\n", (void *) &f.a );
printf( "size of f.a: %zu\n", sizeof f.a );
printf( "address of f.b: %p\n", (void *) &f.b );
printf( "size of f.b: %zu\n", sizeof f.b );
printf( "address of f.c: %p\n", (void *) &f.c );
printf( "size of f.c: %zu\n", sizeof f.c );
Like I said above, the compiler will most likely add padding bytes after f.c, so sizeof f will likely be larger than sizeof f.a + sizeof f.b + sizeof f.c.
&operator. Read up onprintf()& Cos format specifier.printf("%p\n", (void*)&f);because it is more idiomatic, and to ensure the line is sent to output.