OSDN Git Service

05e273e7bf41d49571dea9e7e7d39133e6852b44
[linuxjm/LDP_man-pages.git] / draft / man3 / offsetof.3
1 .\" Copyright (C) 2006 Justin Pryzby <pryzbyj@justinpryzby.com>
2 .\"     and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" Permission is hereby granted, free of charge, to any person obtaining
5 .\" a copy of this software and associated documentation files (the
6 .\" "Software"), to deal in the Software without restriction, including
7 .\" without limitation the rights to use, copy, modify, merge, publish,
8 .\" distribute, sublicense, and/or sell copies of the Software, and to
9 .\" permit persons to whom the Software is furnished to do so, subject to
10 .\" the following conditions:
11 .\"
12 .\" The above copyright notice and this permission notice shall be
13 .\" included in all copies or substantial portions of the Software.
14 .\"
15 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 .\"
23 .\" References:
24 .\"   /usr/lib/gcc/i486-linux-gnu/4.1.1/include/stddef.h
25 .\"   glibc-doc
26 .\"
27 .\" Japanese Version Copyright (c) 2006 Akihiro MOTOKI
28 .\"                             all rights reserved.
29 .\" Translated 2006-07-25, Akihiro MOTOKI, Catch up to LDP v2.34
30 .\"
31 .TH OFFSETOF 3 2008-07-12 "GNU" "Linux Programmer's Manual"
32 .\"O .SH NAME
33 .SH 名前
34 .\"O offsetof \- offset of a structure member
35 offsetof \- 構造体のメンバーのオフセットを返す
36 .\"O .SH SYNOPSIS
37 .SH 書式
38 .nf
39 .B #include <stddef.h>
40 .sp
41 .BI "size_t offsetof(" type ", " member );
42 .fi
43 .\"O .SH DESCRIPTION
44 .SH 説明
45 .\"O The macro
46 .\"O .BR offsetof ()
47 .\"O returns the offset of the field
48 .\"O \fImember\fP from the start of the structure \fItype\fP.
49 .BR offsetof ()
50 マクロは、フィールド \fImember\fP の
51 構造体 \fItype\fP の先頭からのオフセットを返す。
52
53 .\"O This macro is useful because the sizes of the fields that compose
54 .\"O a structure can vary across implementations,
55 .\"O and compilers may insert different numbers of padding
56 .\"O bytes between fields.
57 .\"O Consequently, an element's offset is not necessarily
58 .\"O given by the sum of the sizes of the previous elements.
59 このマクロが有用なのは、
60 構造体を構成するフィールドのサイズは実装によって変化するし、
61 コンパイラによりフィールド間に挿入するパディングのバイト数も
62 違う可能性があるからである。
63 その結果、あるエレメントのオフセットは必ずしもそれより前の
64 エレメントのサイズの合計とはならない。
65
66 .\"O A compiler error will result if
67 .\"O \fImember\fP is not aligned to a byte boundary
68 .\"O (i.e., it is a bit field).
69 \fImember\fP がバイト境界に位置していない場合
70 (すなわち、ビットフィールドの場合) には、
71 コンパイラでエラーが発生する。
72 .\"O .SH "RETURN VALUE"
73 .SH 返り値
74 .\"O .BR offsetof ()
75 .\"O returns the offset of the given
76 .\"O .I member
77 .\"O within the given
78 .\"O .IR type ,
79 .\"O in units of bytes.
80 .BR offsetof ()
81 は、指定された
82 .I member
83 の指定された
84 .I type
85 の中でのオフセットを、バイト単位で返す。
86 .\"O .SH "CONFORMING TO"
87 .SH 準拠
88 C89, C99, POSIX.1-2001.
89 .\"O .SH EXAMPLE
90 .SH 例
91 .\"O On a Linux/i386 system, when compiled using the default
92 .\"O .BR gcc (1)
93 .\"O options, the program below produces the following output:
94 Linux/i386 システムで、
95 .BR gcc (1)
96 のデフォルトオプションで
97 コンパイルされた場合、下記のプログラムは以下のような出力を返す。
98 .in +4n
99 .nf
100
101 .RB "$" " ./a.out"
102 offsets: i=0; c=4; d=8 a=16
103 sizeof(struct s)=16
104 .fi
105 .nf
106 .\"O .SS Program source
107 .SS プログラムのソース
108 \&
109 .nf
110 #include <stddef.h>
111 #include <stdio.h>
112 #include <stdlib.h>
113
114 int
115 main(void)
116 {
117     struct s {
118         int i;
119         char c;
120         double d;
121         char a[];
122     };
123
124 .\"O     /* Output is compiler dependent */
125     /* 出力はコンパイラ依存である */
126
127     printf("offsets: i=%ld; c=%ld; d=%ld a=%ld\\n",
128             (long) offsetof(struct s, i),
129             (long) offsetof(struct s, c),
130             (long) offsetof(struct s, d),
131             (long) offsetof(struct s, a));
132     printf("sizeof(struct s)=%ld\\n", (long) sizeof(struct s));
133
134     exit(EXIT_SUCCESS);
135 }
136 .fi