-
Notifications
You must be signed in to change notification settings - Fork 62
/
8.html
258 lines (249 loc) · 19.5 KB
/
8.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<link href='stylesheets/fonts.css' rel='stylesheet' type='text/css'>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="twitter:creator" content="@lzsthw">
<title>Learn C The Hard Way</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='stylesheets/pure.css' rel='stylesheet'>
<link href='stylesheets/pygments.css' rel='stylesheet'>
<link href='stylesheets/main.css' rel='stylesheet'>
<link href='stylesheets/nav.css' rel='stylesheet'>
<style>
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.11: http://docutils.sourceforge.net/" />
<title>Exercise 8: Sizes And Arrays</title>
</head>
<body id='wrapper'>
<div class='master-logo-wrapper clearfix'>
<a href='index.html'>
<div class='master-logo-sprite'></div>
</a>
<span class='edition-3'><img src='images/beta-edition-cloud.png' /></span>
</div><!-- /.master-logo-wrapper -->
<div style='clear: both;'>
<div id="main">
<div class='chapters-wrapper'>
<nav id='chapters'>
<div class='masthead-title'></div>
<ul class='masthead'>
<li>
<a href='/book/'>
<div class='nav-tcontents'>
<img src='images/nav-contents.png' /></br>
main
</div>
</a>
</li>
<li>
<a href='' id='prev_link'>
<div class='nav-previous'>
<img src='images/nav-previous.png' /></br>
previous
</div>
</a>
</li>
<li>
<a href='' id='next_link'>
<div class='nav-next'>
<img src='images/nav-next.png' /></br>
next
</div>
</a>
</li>
<li><!-- AMBULANCE ICON -->
<a href='help.html' id=''>
<div class='ambulance'>
<img src='images/help-ambulance.png' /></br>
help
</div>
</a>
</li>
<li id="follow">
<a href="https://twitter.com/lzsthw" class="twitter-follow-button" data-show-count="false" data-show-screen-name="false" data-dnt="true">Follow @lzsthw</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
</li>
</ul><!-- /.masthead -->
<!--<img src='images/fa-bullhorn.png' />-->
</nav><!-- /.chapters -->
</div><!-- /.chapters-wrapper -->
<!--- RST STARTS -->
<h1 class="title">Exercise 8: Sizes And Arrays</h1>
<p>In the last exercise you did math, but with a <tt class="docutils literal">'\0'</tt> (nul) character. This
may be odd coming from other languages, since they try to treat "strings" and
"byte arrays" as different beasts. C however treats strings as just arrays of
bytes, and it's only the different printing functions that know there's a
difference.</p>
<p>Before I can really explain the significance of this, I have to introduce a few
more concepts: <tt class="docutils literal">sizeof</tt> and arrays. Here's the code we'll be talking about:</p>
<div class="highlight"><pre><a name="code--ex8.c-pyg.html-1"></a><span class="cp">#include <stdio.h></span>
<a name="code--ex8.c-pyg.html-2"></a>
<a name="code--ex8.c-pyg.html-3"></a><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span>
<a name="code--ex8.c-pyg.html-4"></a><span class="p">{</span>
<a name="code--ex8.c-pyg.html-5"></a> <span class="kt">int</span> <span class="n">areas</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">20</span><span class="p">};</span>
<a name="code--ex8.c-pyg.html-6"></a> <span class="kt">char</span> <span class="n">name</span><span class="p">[]</span> <span class="o">=</span> <span class="s">"Zed"</span><span class="p">;</span>
<a name="code--ex8.c-pyg.html-7"></a> <span class="kt">char</span> <span class="n">full_name</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span>
<a name="code--ex8.c-pyg.html-8"></a> <span class="sc">'Z'</span><span class="p">,</span> <span class="sc">'e'</span><span class="p">,</span> <span class="sc">'d'</span><span class="p">,</span>
<a name="code--ex8.c-pyg.html-9"></a> <span class="sc">' '</span><span class="p">,</span> <span class="sc">'A'</span><span class="p">,</span> <span class="sc">'.'</span><span class="p">,</span> <span class="sc">' '</span><span class="p">,</span>
<a name="code--ex8.c-pyg.html-10"></a> <span class="sc">'S'</span><span class="p">,</span> <span class="sc">'h'</span><span class="p">,</span> <span class="sc">'a'</span><span class="p">,</span> <span class="sc">'w'</span><span class="p">,</span> <span class="sc">'\0'</span>
<a name="code--ex8.c-pyg.html-11"></a> <span class="p">};</span>
<a name="code--ex8.c-pyg.html-12"></a>
<a name="code--ex8.c-pyg.html-13"></a> <span class="c1">// WARNING: On some systems you may have to change the</span>
<a name="code--ex8.c-pyg.html-14"></a> <span class="c1">// %ld in this code to a %u since it will use unsigned ints</span>
<a name="code--ex8.c-pyg.html-15"></a> <span class="n">printf</span><span class="p">(</span><span class="s">"The size of an int: %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
<a name="code--ex8.c-pyg.html-16"></a> <span class="n">printf</span><span class="p">(</span><span class="s">"The size of areas (int[]): %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
<a name="code--ex8.c-pyg.html-17"></a> <span class="k">sizeof</span><span class="p">(</span><span class="n">areas</span><span class="p">));</span>
<a name="code--ex8.c-pyg.html-18"></a> <span class="n">printf</span><span class="p">(</span><span class="s">"The number of ints in areas: %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
<a name="code--ex8.c-pyg.html-19"></a> <span class="k">sizeof</span><span class="p">(</span><span class="n">areas</span><span class="p">)</span> <span class="o">/</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
<a name="code--ex8.c-pyg.html-20"></a> <span class="n">printf</span><span class="p">(</span><span class="s">"The first area is %d, the 2nd %d.</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
<a name="code--ex8.c-pyg.html-21"></a> <span class="n">areas</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">areas</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
<a name="code--ex8.c-pyg.html-22"></a>
<a name="code--ex8.c-pyg.html-23"></a> <span class="n">printf</span><span class="p">(</span><span class="s">"The size of a char: %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="p">));</span>
<a name="code--ex8.c-pyg.html-24"></a> <span class="n">printf</span><span class="p">(</span><span class="s">"The size of name (char[]): %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
<a name="code--ex8.c-pyg.html-25"></a> <span class="k">sizeof</span><span class="p">(</span><span class="n">name</span><span class="p">));</span>
<a name="code--ex8.c-pyg.html-26"></a> <span class="n">printf</span><span class="p">(</span><span class="s">"The number of chars: %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
<a name="code--ex8.c-pyg.html-27"></a> <span class="k">sizeof</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="o">/</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="p">));</span>
<a name="code--ex8.c-pyg.html-28"></a>
<a name="code--ex8.c-pyg.html-29"></a> <span class="n">printf</span><span class="p">(</span><span class="s">"The size of full_name (char[]): %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
<a name="code--ex8.c-pyg.html-30"></a> <span class="k">sizeof</span><span class="p">(</span><span class="n">full_name</span><span class="p">));</span>
<a name="code--ex8.c-pyg.html-31"></a> <span class="n">printf</span><span class="p">(</span><span class="s">"The number of chars: %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
<a name="code--ex8.c-pyg.html-32"></a> <span class="k">sizeof</span><span class="p">(</span><span class="n">full_name</span><span class="p">)</span> <span class="o">/</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="p">));</span>
<a name="code--ex8.c-pyg.html-33"></a>
<a name="code--ex8.c-pyg.html-34"></a> <span class="n">printf</span><span class="p">(</span><span class="s">"name=</span><span class="se">\"</span><span class="s">%s</span><span class="se">\"</span><span class="s"> and full_name=</span><span class="se">\"</span><span class="s">%s</span><span class="se">\"\n</span><span class="s">"</span><span class="p">,</span>
<a name="code--ex8.c-pyg.html-35"></a> <span class="n">name</span><span class="p">,</span> <span class="n">full_name</span><span class="p">);</span>
<a name="code--ex8.c-pyg.html-36"></a>
<a name="code--ex8.c-pyg.html-37"></a> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<a name="code--ex8.c-pyg.html-38"></a><span class="p">}</span>
</pre></div><p>In this code we create a few arrays with different data types in them. Because
arrays of data are so central to how C works, there's a huge number of ways to
create them. For now, just use the syntax <tt class="docutils literal">type name[] = {initializer};</tt> and
we'll explore more. What this syntax means is, "I want an array of type that
is initialized to {..}." When C sees this it does the following:</p>
<ul class="simple">
<li>Look at the type, in this first case it's <tt class="docutils literal">int</tt>.</li>
<li>Look at the <tt class="docutils literal">[]</tt> and see that there's no length given.</li>
<li>Look at the initializer, <tt class="docutils literal">{10, 12, 13, 14, 20}</tt> and
figure out that you want those 5 ints in your array.</li>
<li>Create a piece of memory in the computer, that can hold
5 integers one after another.</li>
<li>Take the name you want, <tt class="docutils literal">areas</tt> and assign it this
location.</li>
</ul>
<p>In the case of <tt class="docutils literal">areas</tt> it's creating an array of 5 ints that contain those
numbers. When it gets to <tt class="docutils literal">char name[] = "Zed";</tt> it's doing the same thing,
except it's creating an array of 3 chars and assigning that to <tt class="docutils literal">name</tt>. The
final array we make is <tt class="docutils literal">full_name</tt>, but we use the annoying syntax of
spelling it out, one character at a time. To C, <tt class="docutils literal">name</tt> and <tt class="docutils literal">full_name</tt> are
identical methods of creating a char array.</p>
<p>The rest of the file, we're using a keyword called <tt class="docutils literal">sizeof</tt> to ask C how big
things are in <em>bytes</em>. C is all about the size and location of pieces of
memory and what you do with them. To help you keep that straight, it gives you
<tt class="docutils literal">sizeof</tt> so you can ask how big something is before you work with it.</p>
<p>This is where stuff gets tricky, so first let's run this and then explain
further.</p>
<div class="section" id="what-you-should-see">
<h1>What You Should See</h1>
<div class="highlight"><pre><a name="code--ex8.sh-session-pyg.html-1"></a><span class="gp">$</span> make ex8
<a name="code--ex8.sh-session-pyg.html-2"></a><span class="go">cc -Wall -g ex8.c -o ex8</span>
<a name="code--ex8.sh-session-pyg.html-3"></a><span class="gp">$</span> ./ex8
<a name="code--ex8.sh-session-pyg.html-4"></a><span class="go">The size of an int: 4</span>
<a name="code--ex8.sh-session-pyg.html-5"></a><span class="go">The size of areas (int[]): 20</span>
<a name="code--ex8.sh-session-pyg.html-6"></a><span class="go">The number of ints in areas: 5</span>
<a name="code--ex8.sh-session-pyg.html-7"></a><span class="go">The first area is 10, the 2nd 12.</span>
<a name="code--ex8.sh-session-pyg.html-8"></a><span class="go">The size of a char: 1</span>
<a name="code--ex8.sh-session-pyg.html-9"></a><span class="go">The size of name (char[]): 4</span>
<a name="code--ex8.sh-session-pyg.html-10"></a><span class="go">The number of chars: 4</span>
<a name="code--ex8.sh-session-pyg.html-11"></a><span class="go">The size of full_name (char[]): 12</span>
<a name="code--ex8.sh-session-pyg.html-12"></a><span class="go">The number of chars: 12</span>
<a name="code--ex8.sh-session-pyg.html-13"></a><span class="go">name="Zed" and full_name="Zed A. Shaw"</span>
<a name="code--ex8.sh-session-pyg.html-14"></a><span class="gp">$</span>
</pre></div><p>Now you see the output of these different <tt class="docutils literal">printf</tt> calls and start to get a
glimpse of what C is doing. Your output could actually be totally different
from mine, since your computer might have different size integers. I'll go
through my output:</p>
<dl class="docutils">
<dt>5</dt>
<dd>My computer thinks an <tt class="docutils literal">int</tt> is 4 bytes in size. Your computer
might use a different size if it's a 32-bit vs. 64-bit.</dd>
<dt>6</dt>
<dd>The <tt class="docutils literal">areas</tt> array has 5 integers in it, so it makes
sense that my computer requires 20 bytes to store it.</dd>
<dt>7</dt>
<dd>If we divide the size of <tt class="docutils literal">areas</tt> by size of an <tt class="docutils literal">int</tt>
then we get 5 elements. Looking at the code, this matches what
we put in the initializer.</dd>
<dt>8</dt>
<dd>We then did an array access to get <tt class="docutils literal">areas[0]</tt> and
<tt class="docutils literal">areas[1]</tt> which means C is "zero indexed" like Python
and Ruby.</dd>
<dt>9-11</dt>
<dd>We repeat this for the <tt class="docutils literal">name</tt> array, but notice
something odd about the size of the array? It says it's
<em>4</em> bytes long, but we only typed "Zed" for 3 characters.
Where's the 4th one coming from?</dd>
<dt>12-13</dt>
<dd>We do the same thing with <tt class="docutils literal">full_name</tt> and
notice it gets this correct.</dd>
<dt>13</dt>
<dd>Finally we just print out the <tt class="docutils literal">name</tt> and <tt class="docutils literal">full_name</tt>
to prove that they actually are "strings" according to printf.</dd>
</dl>
<p>Make sure you can go through and see how these output lines match what was
created. We'll be building on this and exploring more about arrays and storage
next.</p>
</div>
<div class="section" id="how-to-break-it">
<h1>How To Break It</h1>
<p>Breaking this program is fairly easy. Try some of these:</p>
<ul class="simple">
<li>Get rid of the <tt class="docutils literal">'\0'</tt> at the end of <tt class="docutils literal">full_name</tt>
and re-run it. Run it under Valgrind too. Now, move the definition
of <tt class="docutils literal">full_name</tt> to the top of <tt class="docutils literal">main</tt> before <tt class="docutils literal">areas</tt>.
Try running it under Valgrind a few times and see if you get some
new errors. In some cases, you might still get lucky and not catch
any errors.</li>
<li>Change it so that instead of <tt class="docutils literal">areas[0]</tt> you try to
print <tt class="docutils literal">areas[10]</tt> and see what Valgrind thinks of that.</li>
<li>Try other versions of these, doing it to <tt class="docutils literal">name</tt> and
<tt class="docutils literal">full_name</tt> too.</li>
</ul>
</div>
<div class="section" id="extra-credit">
<h1>Extra Credit</h1>
<ul class="simple">
<li>Try assigning to elements in the <tt class="docutils literal">areas</tt> array with <tt class="docutils literal">areas[0] = 100;</tt> and similar.</li>
<li>Try assigning to elements of <tt class="docutils literal">name</tt> and <tt class="docutils literal">full_name</tt>.</li>
<li>Try setting one element of <tt class="docutils literal">areas</tt> to a character from <tt class="docutils literal">name</tt>.</li>
<li>Go search online for the different sizes used for integers on different
CPUs.</li>
</ul>
</div>
<!-- RST ENDS -->
</div><!-- /#main -->
<div class='ad-deck gold' id="footer">
<ul class='retailers clearfix'>
<li>
<a href='http://learnpythonthehardway.org/'>
<div class='retailer-name'>Interested In Python?</div>
<div class='book-type'>Python is also a great language.</div>
<div class='book-price'>Learn Python The Hard Way</div>
</a>
</li>
<li>
<a href='http://learnrubythehardway.org/book/'>
<div class='retailer-name'>Interested In Ruby?</div>
<div class='book-type'>Ruby is also a great language.</div>
<div class='book-price'>Learn Ruby The Hard Way</div>
</a>
</li>
</ul><!-- /.places -->
</div><!-- /#ad-deck -->
<script src="./javascripts/jquery.js"></script>
<script src="./index.js"></script>
<script src="https://paydiv.io/static/jzed.js"></script>
<script src="./javascripts/app.js"></script>
</body>
</html>