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. -->
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
>
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:
24 int a[
10] = {
0,
1,
2,
3,
4,
5,
6,
7,
8,
9};
27 enclosed in braces
<TT>{}
</TT>,
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.
43 int a[
10] = {
0,
1,
2,
3,
4,
5,
6};
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.
52 int b[] = {
10,
11,
12,
13,
14};
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:
63 char s1[
7] =
"Hello,";
64 char s2[
10] =
"there,";
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
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.)
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>
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>