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. -->
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
>
15 <H2>10.5 ``Equivalence'' between Pointers and Arrays
</H2>
17 <p>There are a number of similarities between arrays and pointers in C.
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:
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
38 As we've seen, though,
39 you
<em>can
</em> assign two pointer variables:
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''
59 arrays and pointers are
61 (they are in fact quite different),
64 that they can be used in related ways,
65 and that certain operations may be used between
68 </p><p>The first such operation is that it is possible
69 to (apparently) assign an array to a pointer:
76 In that last assignment
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
83 <TT>ip
</TT> receives a pointer to the first element of
<TT>a
</TT>.
85 it is as if you had written
89 </p><p>The second facet of the equivalence
90 is that you can use the ``array subscripting'' notation
<TT>[i]
</TT>
96 it is just as if you had written
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>
106 using the convenient
<TT>[i]
</TT> notation.
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>,
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>&array[
0]
</TT>.
122 When you write something like
128 it is as if you had written
133 (and you might like to convince yourself of this)
134 gives the same result as if you had written
139 if the character array
143 contains some string,
144 here is another way to find its length:
149 for(p = string; *p != '\
0'; p++)
154 After the loop,
<TT>p
</TT> points to the
<TT>'\
0'
</TT>
155 terminating the string.
159 <TT>p -
&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>.)
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>
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>