From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/types%2Foffsetof.html | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 devdocs/c/types%2Foffsetof.html (limited to 'devdocs/c/types%2Foffsetof.html') diff --git a/devdocs/c/types%2Foffsetof.html b/devdocs/c/types%2Foffsetof.html new file mode 100644 index 00000000..b8739a55 --- /dev/null +++ b/devdocs/c/types%2Foffsetof.html @@ -0,0 +1,39 @@ +

offsetof

Defined in header <stddef.h>
#define offsetof(type, member) /*implementation-defined*/
+

The macro offsetof expands to an integer constant expression 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 subobject, including padding if any.

+

Given an object o of type type with static storage duration, &(o.member) shall be an address constant expression and point to a subobject of o. Otherwise, the behavior is undefined.

+ + +

If the type name specified in type contains a comma not between matching parentheses, the behavior is undefined.

+
(since C23)

Notes

If offsetof is applied to a bit-field member, the behavior is undefined, because the address of a bit-field cannot be taken.

+

member is not restricted to a direct member. It can denote a subobject of a given member, such as an element of an array member.

+

Even though it is specified in C23 that specifying a new type containg an unparenthesized comma in offsetof is undefined behavior, such usage is generally not supported even in earlier modes: offsetof(struct Foo { int a, b; }, a) generally fails to compile.

+ + +

typeof can be used to avoid the bad effect of commas in the definition of a new type, e.g. offsetof(typeof(struct { int i, j; }), i) is well-defined.

+
(since C23)

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

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C standards.

+ + + +
DR Applied to Behavior as published Correct behavior
+DR 496 C89 only structs and struct members were mentioned unions and other subobjects are also supported

See also

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

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/types/offsetof +

+
-- cgit v1.2.3