* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx4aa.html
blob2eff68b8fe358adb11031e22e896f06adf5a2e70
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>4.1.1 Array Initialization</title>
10 <link href="sx4a.html" rev=precedes>
11 <link href="sx4ba.html" rel=precedes>
12 <link href="sx4a.html" rev=subdocument>
13 </head>
14 <body>
15 <H3>4.1.1 Array Initialization</H3>
17 <p>Although it is not possible
18 to assign to all elements of an array at once
19 using an assignment expression,
20 it is possible to initialize some or all elements of an array
21 when the array is defined.
22 The syntax looks like this:
23 <pre>
24 int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
25 </pre>
26 The list of values,
27 enclosed in braces <TT>{}</TT>,
28 separated by commas,
29 provides the initial values for successive elements of the array.
30 </p><p>(Under older, pre-ANSI C compilers,
31 you could not always supply initializers for
32 ``local'' arrays inside functions;
33 you could only initialize ``global'' arrays,
34 those outside of any function.
35 Those compilers are now rare,
36 so you shouldn't have to worry about this distinction any more.
37 We'll talk more about local and global variables
38 later in this chapter.)
39 </p><p>If there are fewer initializers than elements in the array,
40 the remaining elements are automatically initialized to 0.
41 For example,
42 <pre>
43 int a[10] = {0, 1, 2, 3, 4, 5, 6};
44 </pre>
45 would initialize <TT>a[7]</TT>, <TT>a[8]</TT>, and <TT>a[9]</TT> to 0.
46 When an array definition includes an initializer,
47 the array dimension may be omitted,
48 and the compiler will infer the dimension
49 from the number of initializers.
50 For example,
51 <pre>
52 int b[] = {10, 11, 12, 13, 14};
53 </pre>
54 would declare, define, and initialize
55 an array <TT>b</TT> of 5 elements
56 (i.e. just as if you'd typed <TT>int b[5]</TT>).
57 Only the dimension is omitted;
58 the brackets <TT>[]</TT> remain
59 to indicate that <TT>b</TT> is in fact an array.
60 </p><p>In the case of arrays of <TT>char</TT>,
61 the initializer may be a string constant:
62 <pre>
63 char s1[7] = "Hello,";
64 char s2[10] = "there,";
65 char s3[] = "world!";
66 </pre>
68 As before,
70 the dimension is omitted,
71 it is inferred from the size of the string initializer.
72 (We haven't covered strings in detail yet--we'll
73 do so in chapter 8--but
74 it turns out that all strings in C
75 are terminated by a
76 special character with the value 0.
77 Therefore, the array <TT>s3</TT> will be of size 7,
78 and the explicitly-sized <TT>s1</TT>
79 does need to be of size at least 7.
80 For <TT>s2</TT>, the last 4 characters in the array
81 will all end up being this zero-value character.)
82 </p><hr>
83 <p>
84 Read sequentially:
85 <a href="sx4a.html" rev=precedes>prev</a>
86 <a href="sx4ba.html" rel=precedes>next</a>
87 <a href="sx4a.html" rev=subdocument>up</a>
88 <a href="top.html">top</a>
89 </p>
90 <p>
91 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
92 // <a href="copyright.html">Copyright</a> 1995-1997
93 // <a href="mailto:scs@eskimo.com">mail feedback</a>
94 </p>
95 </body>
96 </html>