home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / doc / c / c3 < prev    next >
Encoding:
Text File  |  1975-06-26  |  8.7 KB  |  344 lines

  1. .ta  .5i 1i 1.5i 2i 2.5i 3i
  2. .ul
  3. 8.  Declarations
  4. .et
  5. Declarations are used within function definitions to specify the interpretation
  6. which C gives to each identifier; they do not necessarily
  7. reserve storage associated with the identifier.
  8. Declarations have the form
  9. .dp 2
  10.     declaration:
  11.         decl-specifiers declarator-list\*(op  \fG;
  12. .ed
  13. The declarators in the declarator-list
  14. contain the identifiers being declared.
  15. The decl-specifiers
  16. consist of at most one type-specifier and at most
  17. one storage class specifier.
  18. .dp 5
  19.     decl-specifiers:
  20.         type-specifier
  21.         sc-specifier
  22.         type-specifier sc-specifier
  23.         sc-specifier type-specifier
  24. .ed
  25. 8.1  Storage class specifiers
  26. .et
  27. The sc-specifiers are:
  28. .dp 4
  29.     sc-specifier:
  30. .ft G
  31.         auto
  32.         static
  33.         extern
  34.         register
  35. .ed
  36. The
  37. .bd "auto, static,"
  38. and
  39. .bd register
  40. declarations also serve as definitions
  41. in that they cause an appropriate amount of storage to be reserved.
  42. In the \fGextern\fR case
  43. there must be an external definition (see below)
  44. for the given identifiers
  45. somewhere outside the function in which they are declared.
  46. .pg
  47. There are some severe restrictions on
  48. .bd register
  49. identifiers:
  50. there can be at most 3 register identifiers in any function,
  51. and the type of a register identifier can only be
  52. .bd int,
  53. .bd char,
  54. or pointer (not
  55. .bd float,
  56. .bd double,
  57. structure, function, or array).
  58. Also the address-of
  59. operator
  60. .bd &
  61. cannot be applied to such identifiers.
  62. .a
  63. Except for these restrictions
  64. (in return for which one is rewarded with faster, smaller code),
  65. register identifiers behave as if they
  66. were automatic.
  67. In fact implementations of C
  68. are free to treat
  69. .bd register
  70. as synonymous with
  71. .bd auto.
  72. .pg
  73. If the sc-specifier is missing from a declaration, it
  74. is generally taken to be \fGauto\fR.
  75. .ms
  76. 8.2  Type specifiers
  77. .et
  78. The type-specifiers are
  79. .dp 6
  80.     type-specifier:
  81.         \fGint\fR
  82.         \fGchar\fR
  83.         \fGfloat
  84.         \fGdouble
  85.         struct \fI{ type-decl-list }\fG
  86.         struct \fIidentifier { type-decl-list }\fG
  87.         struct \fIidentifier
  88. .ed
  89. The \fGstruct\fR specifier is discussed in \(sc8.5.
  90. If the type-specifier is missing from a declaration,
  91. it is generally taken to be \fGint\fR.
  92. .ms
  93. 8.3  Declarators
  94. .et
  95. The declarator-list appearing in a declaration
  96. is a comma-separated sequence of declarators.
  97. .dp 2
  98.     declarator-list:
  99.         declarator
  100.         declarator \fG,\fI declarator-list
  101. .ed
  102. The specifiers in the declaration
  103. indicate the type and storage class of the objects to which the
  104. declarators refer.
  105. Declarators have the syntax:
  106. .dp 6
  107.     declarator:
  108.         identifier
  109.         \fG\**\fI declarator
  110.         declarator \fG( )\fI
  111.         declarator \fG[ \fIconstant-expression\*(op \fG]
  112.         \fG( \fIdeclarator \fG)
  113. .ed
  114. The grouping in this definition is
  115. the same as in expressions.
  116. .ms
  117. 8.4  Meaning of declarators
  118. .et
  119. Each declarator is taken to be
  120. an assertion that when a construction of
  121. the same form as the declarator appears in an expression,
  122. it yields an object of the indicated
  123. type and storage class.
  124. Each declarator contains exactly one identifier; it is this identifier that
  125. is declared.
  126. .pg
  127. If an unadorned identifier appears
  128. as a declarator, then it has the type
  129. indicated by the specifier heading the declaration.
  130. .pg
  131. If a declarator has the form
  132. .sp .7
  133.     \** D
  134. .sp .7
  135. for D a declarator, then the
  136. contained identifier has the type ``pointer to .^.^.'', where
  137. ``^.^.^.^'' is the type which the identifier would have had
  138. if the declarator had been simply D.
  139. .pg
  140. If a declarator has the form
  141. .sp .7
  142.     D^(^^)
  143. .sp .7
  144. then the contained identifier has the type
  145. ``function returning ...'', where ``^.^.^.^'' is the
  146. type which the identifier would have
  147. had if the declarator had been simply D.
  148. .pg
  149. A declarator may have the form
  150. .sp .7
  151.     D[constant-expression]
  152. .sp .3
  153. or
  154. .sp .3
  155.     D[^^]
  156. .sp .7
  157. In the first case the constant
  158. expression
  159. is an expression
  160. whose value is determinable at compile time,
  161. and whose type is
  162. .ft G
  163. int.
  164. .ft R
  165. in the second the constant 1 is used.
  166. (Constant expressions are defined precisely in \(sc15.)^^
  167. Such a declarator makes the contained identifier have
  168. type ``array.''
  169. If the unadorned declarator D would
  170. specify a non-array of type ``.^.^.'',
  171. then the declarator
  172. ``D[^i^]''
  173. yields a 1-dimensional array with rank \fIi\fR of objects
  174. of type ``.^.^.''.
  175. If the unadorned declarator D would
  176. specify an \fIn^\fR-dimensional array
  177. with rank
  178. .ft I
  179. i\s6\d1\u\s10^\(mu^i\s6\d2\u\s10^\(mu^.^.^.^\(mu^i\s6\dn\u\s10,
  180. .ft R
  181. then the declarator ``D[^i\s6\dn+1\u\s10^]''
  182. yields an (\fIn^\fR+1^)\fR^-dimensional
  183. array with rank
  184. .ft I
  185. i\s6\d1\u\s10^\(mu^i\s6\d2\u\s10^\(mu^.^.^.^\(mu^i\s6\dn\u\s10\
  186. ^\(mu^i\s6\dn+1\u\s10\fR.
  187. .pg
  188. An array may be constructed from one of the basic types, from a pointer,
  189. from a structure,
  190. or from another array (to generate a multi-dimensional array).
  191. .pg
  192. Finally, parentheses in declarators do not alter the type
  193. of the contained identifier
  194. except insofar as they alter the binding
  195. of the components of the declarator.
  196. .pg
  197. Not all the possibilities
  198. allowed by the syntax above are actually
  199. permitted.
  200. The restrictions are as follows:
  201. functions may not return
  202. arrays, structures or functions,
  203. although they may return pointers to such things;
  204. there are no arrays of functions, although
  205. there may be arrays of pointers to functions.
  206. Likewise a structure may not contain a function,
  207. but it may contain a pointer to a function.
  208. .pg
  209. As an example, the declaration
  210. .sp .7
  211. .bG
  212.     int i, \**ip, f^(^^), \**fip(^^), (\**pfi)^(^^);
  213. .eG
  214. .sp .7
  215. declares an integer \fIi\fR,
  216. a pointer \fIip\fR to an integer,
  217. a function \fIf\fR returning an integer,
  218. a function \fIfip\fR returning a pointer to an integer,
  219. and a pointer \fIpfi\fR to a function which
  220. returns an integer.
  221. Also
  222. .sp .7
  223. .bG
  224.     float fa[17], \**afp[17];
  225. .eG
  226. .sp .7
  227. declares an array of \fGfloat\fR numbers and an array of
  228. pointers to \fGfloat\fR numbers.
  229. Finally,
  230. .sp .7
  231. .bG
  232.     static int x3d[3][5][7];
  233. .sp .7
  234. .eG
  235. declares a static three-dimensional array of integers,
  236. with rank 3\(mu5\(mu7.
  237. In complete detail, \fIx3d\fR is an array of three items:
  238. each item is an array of five arrays;
  239. each of the latter arrays is an array of seven
  240. integers.
  241. Any of the expressions ``x3d'', ``x3d[^i^]'', ``x3d[^i^][^j^]'', ``x3d[^i^][^j^][^k^]''
  242. may reasonably appear in an expression.
  243. The first three have type ``array'',
  244. the last has type \fGint\fR.
  245. .ms
  246. 8.5  Structure declarations
  247. .et
  248. Recall that one of the forms for a structure specifier
  249. is
  250. .dp
  251.         \fGstruct \fI{ type-decl-list }
  252. .ed
  253. The
  254. .ft I
  255. type-decl-list
  256. .ft R
  257. is a sequence of type declarations for the members of the structure:
  258. .dp 3
  259.     type-decl-list:
  260.         type-declaration
  261.         type-declaration type-decl-list
  262. .ed
  263. A type declaration is just a declaration which does not
  264. mention a storage class (the storage class ``member of
  265. structure'' here being understood by context).
  266. .dp 2
  267.     type-declaration:
  268.         type-specifier declarator-list  \fG;
  269. .ed
  270. Within the structure, the objects declared
  271. have addresses which increase as their declarations
  272. are read left-to-right.
  273. Each component of a structure
  274. begins on an addressing boundary appropriate
  275. to its type.
  276. On the \s8PDP\s10-11 the only requirement is that non-characters
  277. begin on a word boundary; therefore, there may
  278. be 1-byte, unnamed holes in a structure,
  279. and all structures
  280. have an even length in bytes.
  281. .pg
  282. Another form of structure specifier is
  283. .dp 1
  284.         \fGstruct \fIidentifier { type-decl-list }
  285. .ed
  286. This form is the same as the one just discussed,
  287. except that the identifier is remembered
  288. as the
  289. .ft I
  290. structure tag
  291. .ft R
  292. of the structure specified by the list.
  293. A subsequent declaration may then be given
  294. using the structure tag but without the list,
  295. as in the third form of structure specifier:
  296. .dp
  297.         \fGstruct \fIidentifier
  298. .ed
  299. Structure tags allow definition of self-referential
  300. structures; they also
  301. permit the long part of the declaration to be
  302. given once and used several times.
  303. It is however absurd to declare a structure
  304. which contains an instance of
  305. itself, as distinct from a pointer to an instance of itself.
  306. .pg
  307. A simple example of a structure declaration, taken from
  308. \(sc16.2 where its use is illustrated more fully, is
  309. .dp 6
  310. .bG
  311.     struct tnode {
  312.         char tword[20];
  313.         int count;
  314.         struct tnode \**left;
  315.         struct tnode \**right;
  316.     };
  317. .ed
  318. .eG
  319. which contains an array of 20 characters, an integer, and two pointers
  320. to similar structures.
  321. Once this declaration has been given, the following
  322. declaration makes sense:
  323. .sp .7
  324. .bG
  325.     struct tnode s, \**sp;
  326. .br
  327. .eG
  328. .sp .7
  329. which declares
  330. \fIs\fR to be a structure of the given sort
  331. and \fIsp\fR to be a pointer to a structure
  332. of the given sort.
  333. .pg
  334. The names of structure members and structure tags may
  335. be the same as ordinary variables, since a distinction
  336. can be made by context.
  337. However, names of tags and members
  338. must be distinct.
  339. The same member name can appear in different
  340. structures only if the two members are of the same type
  341. and if their origin with respect to their structure is the
  342. same;
  343. thus separate structures can share a common initial segment.
  344.