offsetof is what you want for this and it compile time. The C99 draft standard section 7.17
Common definitions paragraph 3 says:
offsetof(type, member-designator)
which expands to an integer constant expression that has type size_t, the value of
which is the offset in bytes [...]
the man pages linked above has the following sample code, which demonstrates it's usage:
struct s {
int i;
char c;
double d;
char a[];
};
/* Output is compiler dependent */
printf("offsets: i=%ld; c=%ld; d=%ld a=%ld
",
(long) offsetof(struct s, i),
(long) offsetof(struct s, c),
(long) offsetof(struct s, d),
(long) offsetof(struct s, a));
printf("sizeof(struct s)=%ld
", (long) sizeof(struct s));
sample output, which may vary:
offsets: i=0; c=4; d=8 a=16
sizeof(struct s)=16
For reference constant expressions are covered in section 6.6
Constant expressions and paragraph 2 says:
A constant expression can be evaluated during translation rather than runtime, and
accordingly may be used in any place that a constant may be.
Update
After clarification from the OP I came up with a new methods using -O0 -fverbose-asm -S
and grep, code is as follows:
#include <stddef.h>
struct s {
int i;
char c;
double d;
char a[];
};
int main()
{
int offsetArray[4] = { offsetof(struct s, i ), offsetof( struct s, c ), offsetof(struct s, d ), offsetof(struct s, a ) } ;
return 0 ;
}
build using:
gcc -x c -std=c99 -O0 -fverbose-asm -S main.cpp && cat main.s | grep offsetArray
sample output (live example):
movl $0, -16(%rbp) #, offsetArray
movl $4, -12(%rbp) #, offsetArray
movl $8, -8(%rbp) #, offsetArray
movl $16, -4(%rbp) #, offsetArray