* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx10e.html
blobf9c22c37174fe5e0dd67b709e98ae3c31a1195a8
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 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>10.5 ``Equivalence'' between Pointers and Arrays</title>
10 <link href="sx10d.html" rev=precedes>
11 <link href="sx10f.html" rel=precedes>
12 <link href="sx10.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>10.5 ``Equivalence'' between Pointers and Arrays</H2>
17 <p>There are a number of similarities between arrays and pointers in C.
18 If you have an array
19 <pre>
20 int a[10];
21 </pre>
22 you can refer to <TT>a[0]</TT>, <TT>a[1]</TT>, <TT>a[2]</TT>, etc.,
23 or to <TT>a[i]</TT> where <TT>i</TT> is an <TT>int</TT>.
24 If you declare a pointer variable <TT>ip</TT>
25 and set it to point to the beginning of an array:
26 <pre>
27 int *ip = &amp;a[0];
28 </pre>
29 you can refer to <TT>*ip</TT>, <TT>*(ip+1)</TT>, <TT>*(ip+2)</TT>, etc.,
30 or to <TT>*(ip+i)</TT> where <TT>i</TT> is an <TT>int</TT>.
31 </p><p>There are also differences, of course.
32 You cannot assign two arrays; the code
33 <pre>
34 int a[10], b[10];
35 a = b; /* WRONG */
36 </pre>
37 is illegal.
38 As we've seen, though,
39 you <em>can</em> assign two pointer variables:
40 <pre>
41 int *ip1, *ip2;
42 ip1 = &amp;a[0];
43 ip2 = ip1;
44 </pre>
46 Pointer assignment is straightforward;
47 the pointer on the left is simply made to point wherever the
48 pointer on the right does.
49 We haven't copied the data pointed to
50 (there's still just one copy, in the same place);
51 we've just made two pointers point to that one place.
52 </p><p>The similarities between arrays and pointers
53 end up being quite useful,
54 and in fact C builds on the similarities,
55 leading to what is called
56 ``the equivalence of arrays and pointers in C.''
57 When we speak of this ``equivalence''
58 we do not mean that
59 arrays and pointers are
60 the same thing
61 (they are in fact quite different),
62 but
63 rather
64 that they can be used in related ways,
65 and that certain operations may be used between
67 them.
68 </p><p>The first such operation is that it is possible
69 to (apparently) assign an array to a pointer:
70 <pre>
71 int a[10];
72 int *ip;
73 ip = a;
74 </pre>
75 What can this mean?
76 In that last assignment
77 <TT>ip = a</TT>,
78 aren't we mixing apples and oranges again?
79 It turns out that we are not;
80 C defines the result of this assignment
81 to be
82 that
83 <TT>ip</TT> receives a pointer to the first element of <TT>a</TT>.
84 In other words,
85 it is as if you had written
86 <pre>
87 ip = &amp;a[0];
88 </pre>
89 </p><p>The second facet of the equivalence
90 is that you can use the ``array subscripting'' notation <TT>[i]</TT>
91 on pointers, too.
92 If you write
93 <pre>
94 ip[3]
95 </pre>
96 it is just as if you had written
97 <pre>
98 *(ip + 3)
99 </pre>
100 So when you have a pointer that points to a block of memory,
101 such as an array or a part of an array,
102 you can treat that pointer
103 ``as if'' it <em>were</em>
105 an array,
106 using the convenient <TT>[i]</TT> notation.
107 In other words,
108 at the beginning of this section when we talked about
109 <TT>*ip</TT>, <TT>*(ip+1)</TT>, <TT>*(ip+2)</TT>,
110 and <TT>*(ip+i)</TT>,
111 we could have written
112 <TT>ip[0]</TT>, <TT>ip[1]</TT>, <TT>ip[2]</TT>,
113 and <TT>ip[i]</TT>.
114 As we'll see, this can be quite useful
115 (or at least convenient).
116 </p><p>The third facet of the equivalence
117 (which is actually a more general version of the first one we mentioned)
118 is that <em>whenever</em> you mention the name of an array
119 in a context where the ``value'' of the array would be needed,
120 C automatically generates a pointer to the first element of the array,
121 as if you had written <TT>&amp;array[0]</TT>.
122 When you write something like
123 <pre>
124 int a[10];
125 int *ip;
126 ip = a + 3;
127 </pre>
128 it is as if you had written
129 <pre>
130 ip = &amp;a[0] + 3;
131 </pre>
132 which
133 (and you might like to convince yourself of this)
134 gives the same result as if you had written
135 <pre>
136 ip = &amp;a[3];
137 </pre>
138 </p><p>For example,
139 if the character array
140 <pre>
141 char string[100];
142 </pre>
143 contains some string,
144 here is another way to find its length:
145 <pre>
146 int len;
147 char *p;
149 for(p = string; *p != '\0'; p++)
152 len = p - string;
153 </pre>
154 After the loop, <TT>p</TT> points to the <TT>'\0'</TT>
155 terminating the string.
156 The expression
157 <TT>p - string</TT>
158 is equivalent to
159 <TT>p - &amp;string[0]</TT>,
160 and gives the length of the string.
161 (Of course, we could also call <TT>strlen</TT>;
162 in fact here we've essentially written
163 another implementation of <TT>strlen</TT>.)
164 </p><hr>
166 Read sequentially:
167 <a href="sx10d.html" rev=precedes>prev</a>
168 <a href="sx10f.html" rel=precedes>next</a>
169 <a href="sx10.html" rev=subdocument>up</a>
170 <a href="top.html">top</a>
171 </p>
173 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
174 // <a href="copyright.html">Copyright</a> 1995, 1996
175 // <a href="mailto:scs@eskimo.com">mail feedback</a>
176 </p>
177 </body>
178 </html>