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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
{
"metadata": {
"name": "",
"signature": "sha256:b382733cd221154cfe7c9ffe63477448084168d501d248c2b1d0c253ed011821"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Chapter 6: Pointers<h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.1, Page Number: 107<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from ctypes import *\n",
"\n",
"balance =c_int(3200) #int variable\n",
"balptr=pointer(balance) #pointer to int\n",
"value=balptr[0] #accessing the value using the pointer\n",
"\n",
"#Result\n",
"print \"balance: \",value \n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"balance: 3200\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.2, Page Number: 109<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"#Variable declaration\n",
"x=123.23\n",
"y=c_double()\n",
"p=POINTER(c_int)\n",
"\n",
"p=pointer(c_int(int(x))) #type case double to int\n",
"y=p[0]\n",
"\n",
"#Result\n",
"print y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"123\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.3, Page Number: 110<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from ctypes import *\n",
"\n",
"#Variable declaration\n",
"num=c_int() #declaring int\n",
"p=pointer(num) #pointer to int\n",
"\n",
"p[0]=100\n",
"print num.value,\n",
"p[0]+=1\n",
"print num.value,\n",
"p[0]-=1\n",
"print num.value"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100 101 100\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.4, Page Number: 111<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"from ctypes import *\n",
"\n",
"#Variable declaration\n",
"j=c_int()\n",
"g=c_double()\n",
"i=pointer(j)\n",
"f=pointer(g)\n",
"\n",
"\n",
"for x in range(10):\n",
" print addressof(i.contents)+x,addressof(f.contents)+x\n",
" \n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"84638480 84639248\n",
"84638481 84639249\n",
"84638482 84639250\n",
"84638483 84639251\n",
"84638484 84639252\n",
"84638485 84639253\n",
"84638486 84639254\n",
"84638487 84639255\n",
"84638488 84639256\n",
"84638489 84639257\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.5, Page Number: 114<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from ctypes import *\n",
"\n",
"#Variable declaration\n",
"str=\"This is a test\"\n",
"token =\"\"\n",
"i=0\n",
"\n",
"#Read a token at a time from the string\n",
"while i<len(str):\n",
" token=c_char_p(\"\") #set token to null string\n",
" q=pointer(token) \n",
" #Read characters until either a space or the null terminator is encountered'''\n",
" while i<len(str) and not(str[i]==\" \"):\n",
" q[0]+=str[i]\n",
" i+=1\n",
" i+=1 #advance past the space\n",
" print q[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"This\n",
"is\n",
"a\n",
"test\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.6, Page Number: 115<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"str=\"This is a test\"\n",
"i=0\n",
"\n",
"#Read a token at a time from the string\n",
"while i<len(str):\n",
" token=\"\" #set q to null string\n",
" #Read characters until either a space or the null terminator is encountered'''\n",
" while i<len(str) and not(str[i]==\" \"):\n",
" token+=str[i]\n",
" i+=1\n",
" i+=1 #advance past the space\n",
" print token"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"This\n",
"is\n",
"a\n",
"test\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.7, Page Number: 115<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"import string\n",
"\n",
"str=c_char_p(\"hello tom\")\n",
"q=\"\"\n",
"p=pointer(str) #put address of str into p\n",
" \n",
"p[0]=string.upper(p[0])\n",
"\n",
"#Result\n",
"print p[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"HELLO TOM\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.8, Page Number: 117<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"#Varible declaration\n",
"s=\"Pointers are fun to use\"\n",
"\n",
"#Result\n",
"print s\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Pointers are fun to use\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.9, Page Number: 118<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from ctypes import *\n",
"\n",
"#Variable declaration\n",
"num=[]\n",
"\n",
"#User input\n",
"for i in range(10):\n",
" num.append(i)\n",
" \n",
"start=num #set start to the starting pointer \n",
"for i in range(10):\n",
" print start[i],\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0 1 2 3 4 5 6 7 8 9\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.10, Page Number: 119<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"import random\n",
"\n",
"fortunes=[\"Soon, you will come into some money.\",\n",
" \"A new love will enter your lifr. \",\n",
" \"You will live long and prosper.\",\n",
" \"Now is a good time to invest for the future.\",\n",
" \"A close friend will ask for a favour.\"]\n",
"\n",
"print \"To see your fortune, press a key: \"\n",
"\n",
"#Randomize the random number generator\n",
"chance=random.randint(0,100)\n",
"chance=chance%5\n",
"\n",
"#Result\n",
"print fortunes[chance]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"To see your fortune, press a key: \n",
"A close friend will ask for a favour.\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.11, Page Number: 120<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"keyword=[[\"for\",\"for(initialization; condition; increment\"],\n",
" [\"if\",\"if(condition) ... else ...\"],\n",
" [\"switch\",\"switch(value) { case-list }\"],\n",
" [\"while\",\"while(condition) ...\"],\n",
" [\"\",\"\"]] #Terminates the list with nulls\n",
" \n",
"#User input \n",
"print \"Enter keyword: \"\n",
"str=\"for\" \n",
"\n",
"for i in range(4):\n",
" if str==keyword[i][0]:\n",
" print keyword[i][1]\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter keyword: \n",
"for(initialization; condition; increment\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.12, Page Number: 123<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"from ctypes import *\n",
"\n",
"#Variable declaration\n",
"x=c_int(10) #int variable\n",
"p=pointer(x) #pointer to int\n",
"q=pointer(p) #pointer to a pointer\n",
"\n",
"#Result\n",
"print q[0][0] #accessing the value using a pointer to a pointer\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 6.13, Page Number: 126<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from ctypes import *\n",
"\n",
"#Variable declaration\n",
"s=c_char_p()\n",
"p1=pointer(s)\n",
"x=0\n",
"\n",
"while True:\n",
" print \"\\nEnter a string: \",\n",
" if x==2:\n",
" p1[0]=c_char_p(\"done\")\n",
" else:\n",
" p1[0]=c_char_p(\"Hello\")\n",
" #print the ASCII values of each characcter\n",
" for i in range(0,len(p1[0])):\n",
" print ord(p1[0][i]),\n",
" x+=1\n",
" if p1[0]==\"done\":\n",
" break\n",
" \n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter a string: 72 101 108 108 111 \n",
"Enter a string: 72 101 108 108 111 \n",
"Enter a string: 100 111 110 101\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|