Namespaces
Variants
Views
Actions

offsetof

From cppreference.com
< c‎ | types
Revision as of 04:43, 4 June 2014 by Newatthis (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Defined in header <stddef.h>
#define offsetof(type, member) /*implementation-defined*/

The macro offsetof expands to a constant of type size_t, the value of which is the offset, in bytes, from the beginning of an object of specified type to its specified member, including padding if any.

[edit] Example

#include <stdio.h>
#include <stddef.h>
 
struct S {
    char c;
    double d;
};
 
int main(void)
{
    printf("the first element is at offset %zu\n", offsetof(struct S, c));
    printf("the double is at offset %zu\n", offsetof(struct S, d));
}

Possible output:

the first element is at offset 0
the double is at offset 8

[edit] See also

unsigned integer type returned by the sizeof operator
(typedef) [edit]
C++ documentation for offsetof