diff options
Diffstat (limited to 'devdocs/c/types%2Foffsetof.html')
| -rw-r--r-- | devdocs/c/types%2Foffsetof.html | 39 |
1 files changed, 39 insertions, 0 deletions
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 @@ + <h1 id="firstHeading" class="firstHeading">offsetof</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stddef.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">#define offsetof(type, member) /*implementation-defined*/</pre> +</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>The macro <code>offsetof</code> expands to an <a href="../language/constant_expression#Integer_constant_expression" title="c/language/constant expression">integer constant expression</a> of type <code><a href="size_t" title="c/types/size t">size_t</a></code>, 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.</p> +<p>Given an object <code>o</code> of type <code>type</code> with static storage duration, <code>&(o.member)</code> shall be an address constant expression and point to a subobject of <code>o</code>. Otherwise, the behavior is undefined.</p> +<table class="t-rev-begin"> <tr class="t-rev t-since-c23"> +<td> <p>If the type name specified in <code>type</code> contains a comma not between matching parentheses, the behavior is undefined.</p> +</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td> +</tr> </table> <h3 id="Notes"> Notes</h3> <p>If <code>offsetof</code> is applied to a bit-field member, the behavior is undefined, because the address of a bit-field cannot be taken.</p> +<p><code>member</code> is not restricted to a direct member. It can denote a subobject of a given member, such as an element of an array member.</p> +<p>Even though it is specified in C23 that specifying a new type containg an unparenthesized comma in <code>offsetof</code> is undefined behavior, such usage is generally not supported even in earlier modes: <code>offsetof(struct Foo { int a, b; }, a)</code> generally fails to compile.</p> +<table class="t-rev-begin"> <tr class="t-rev t-since-c23"> +<td> <p><a href="../language/typeof" title="c/language/typeof"><code>typeof</code></a> can be used to avoid the bad effect of commas in the definition of a new type, e.g. <code>offsetof(typeof(struct { int i, j; }), i)</code> is well-defined.</p> +</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td> +</tr> </table> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#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)); +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">the first element is at offset 0 +the double is at offset 8</pre></div> </div> <h3 id="Defect_reports"> Defect reports</h3> <p>The following behavior-changing defect reports were applied retroactively to previously published C standards.</p> +<table class="dsctable"> <tr> <th>DR </th> <th>Applied to </th> <th>Behavior as published </th> <th>Correct behavior </th> +</tr> <tr> <td> +<a rel="nofollow" class="external text" href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_496">DR 496</a> </td> <td>C89 </td> <td>only structs and struct members were mentioned </td> <td>unions and other subobjects are also supported </td> +</tr> +</table> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="size_t" title="c/types/size t"> <span class="t-lines"><span>size_t</span></span></a></div> </td> <td> unsigned integer type returned by the <a href="../language/sizeof" title="c/language/sizeof"><code>sizeof</code></a> operator <br> <span class="t-mark">(typedef)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/types/offsetof" title="cpp/types/offsetof">C++ documentation</a></span> for <code>offsetof</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/types/offsetof" class="_attribution-link">https://en.cppreference.com/w/c/types/offsetof</a> + </p> +</div> |
