Chapter Contents |
Previous |
Next |
offsetof |
Portability: | ISO/ANSI C conforming |
SYNOPSIS | |
DESCRIPTION | |
RETURN VALUE | |
EXAMPLES |
SYNOPSIS |
#include <stddef.h> /* macro */ size_t offsetof(type, element)
DESCRIPTION |
The
offsetof
macro provides the decimal byte offset of a component within a structure
as a
size_t
constant. This constant is
generated at compile time. Padding for alignment, if any, is included. The
operands of
offsetof
are a structure type
(
type
) and a component of the structure
specification (
element
). The component
specification does not include the structure type or the selection operators
.
or
->
.
RETURN VALUE |
offsetof
returns the byte offset of
element
.
EXAMPLES |
As shown in these examples, you should
write the member specification as it would be written to access the value
of a structure member, except that there is no leading
.
or
->
selection operator.
#include <stddef.h> struct AAA { /* Define structure AAA. */ double ddd; char ccc; int bbb; }; long x; /* x is the byte offset of component bbb in struct AAA. */ x = offsetof(struct AAA, bbb);
Example 1.2 shows a structure,
data
, with an inner structure
base
.
#include <stddef.h> struct data { /* Define struct data. */ int id; int *elem; char *name; struct { /* Define struct type base. */ double proj; } base; }; long ofs; /* ofs is the byte offset of base.proj. */ ofs = offsetof(struct data, base.proj);
In Example 1.3,
complex
is defined via a
typedef
statement
to be a structure type. The component specification
inner.d[5]
specifies an array element within an inner structure. The
variable
y
is set to the offset of the
sixth array element in the inner structure (
decimal
56
).
#include <stddef.h> typedef struct { /* Define struct type complex. */ struct XXX *xptr, *xptr2; struct { /* Define struct type inner. */ int count, count2; double d[10]; } inner; struct XXX *xptr3; } complex; /* y is the byte offset of inner.d[5]. */ long y; y = offsetof(complex, inner.d[5]);
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.