forked from raghuprathap/varodek.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
2658 lines (2051 loc) · 120 KB
/
index.html
File metadata and controls
2658 lines (2051 loc) · 120 KB
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-113384250-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-113384250-1');
</script>
<title>Learn coding by coding</title>
<link rel="stylesheet" type="text/css" href="p.css">
</head>
<body>
<!-- **************************************************TABLE 1******************************************************************************************************-->
<table width=100% cellspacing=5 id="intro">
<!-- *********************ROW 1*********************************-->
<tr>
<th> <h3></h3></th>
<th>
<div style="font-size:2em;font-variant:normal;font-weight:normal;font-family: Times New Roman, Times, serif;"><u><h1 align="center">Pointers in C/C++</h1></u></div>
</th>
<th ><h3></h3></th>
</tr>
<!--*********************ROW 2*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Basics</u></h1>
<div style="border: 1px solid black; border-radius:10px; font-size:1em; font-variant:normal; font-weight:normal; font-family: Times New Roman, Times, serif; padding:5px" ><h3 align="center"><u>Important</u></h3><p>The codes in examples are executed on Linux (Ubuntu 16.04 <b>64 bit</b>) with <b>g++ version 5.4.0</b> (thread model- <b>Posix</b>).</p><p>Here, when we refer to 'memory', it means we are talking about only RAM, as program is executed here only.</p></div>
<p>Before understanding pointers, first understand how memory is handled by C/C++ compiler.</p>
<p>Let us see a loosely connected example. Consider these situations.</p>
<ol type="1">
<li> There are a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>,...........,a<sub>n</sub>, houses in a city.</li>
<li> The task is to deliver letters from post-office to these houses.</li>
<li> One option is to allot each house to each b<sub>1</sub>, b<sub>2</sub>, b<sub>3</sub>,...........,b<sub>n</sub>, post-men and give them job to deliver letters.</li>
<li> Second option is to allot each house, a unique address and then tell a single post-man to deliver a particular letter to the particular house using those unique addresses.</li>
</ol>
<p>The post-men in first option are our normal variables defined in our code</p>
<p>The post-man in second option is our pointer variable defined in our code.</p>
<p><b>A pointer is a variable which stores the address of another variable of same-data-type, to access their values.</b></p>
</td>
<td class="adv"></td>
</tr>
<!--**********************ROW 3**************************************-->
</table>
<!-- **************************************************INDEX******************************************************************************************************-->
<table width=100% cellspacing="5">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Index</u></h1>
<ol type="1">
<li><a href="#intro">Basics</a></li>
<li><a href="#variables">Variables</a></li>
<li><a href="#address_of_operator">Address-of Operator (&)</li>
<li><a href="#declaring_pointer">Declaring a pointer</a></li>
<li><a href="#initializing_pointer">Storing address in a Pointer</a></li>
<li><a href="#dereference">De-reference/ Indirection Operator</a></li>
<li><a href="#double_pointer">Pointer to a pointer or Pointer to pointer or Double Pointer</a></li>
<li><a href="#size_of_pointer">Size of Pointer</a></li>
<li><a href="#pointer_to_array">Pointer pointing to an array</a></li>
<li><a href="#pointer++">Arithmetic operations on Pointers</a></li>
<li><a href="#more">Some more Results</a></li>
<li><a href="#types">Different types of Pointers</a></li>
<ol type="a">
<li><a href="#int*">int *ptr</li>
<li><a href="#const_int*">const int *ptr</a></li>
<li><a href="#const_int*">int const *ptr</a></li>
<li><a href="#*const">int *const ptr</a></li>
<li><a href="#const_int_const*">const int *const ptr</a></li>
</ol>
<li><a href="#2d">Implementation of 2-D array</a></li>
<li><a href="#point_2d">Pointer to a 2-D array</a></li>
<li><a href="#dynamic">Dynamic Allocation</a></li>
<li><a href="#malloc">malloc()</a></li>
<li><a href="#calloc">calloc()</a></li>
<li><a href="#realloc">realloc()</a></li>
<li><a href="#free">free()</a></li>
<li><a href="#use_malloc">Implementing malloc()</a></li>
<li><a href="#dynamic_1d">Dynamic allocation of 1-D array</a></li>
<li><a href="#dynamic_1d_calloc">Dynamic allocation of 1-D array using calloc()</a></li>
<li><a href="#dynamic_1d_realloc">Dynamic re-allocation of 1-D array using realloc()</a></li>
<li><a href="#dynamic_2d">Dynamic allocation of 2-D array</a></li>
<li><a href="#NULL">Wild pointer and importance of NULL</a></li>
</ol>
</td>
<td class="adv"></td>
</tr>
</table>
<!-- **************************************************TABLE 2******************************************************************************************************-->
<table width=100% cellspacing="5" id="variables">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Variables</u></h1>
<p>Variables are locations in the computer's memory which can be accessed by their identifier (their name). This way, the program does not need to care about the physical address of the data in memory; it simply uses the identifier whenever it needs to refer to the variable. Or we can say that whenever we run the code, the memory is allotted to variables and their addresses are assigned to the identifiers.
When a variable is initialized, the memory needed to store its value is allocated at a specific location in memory.</p>
<p>Generally, C/C++ programs do not actively decide the exact memory addresses where its variables are stored. Fortunately, that task is left to the environment where the program is run - generally, an operating system that decides the particular memory locations on runtime. However, it may be useful for a program to be able to obtain the address of a variable during runtime in order to access data cells that are at a certain position relative to it. </p>
</td>
<td class="adv"></td>
</tr>
</table>
<!-- **************************************************TABLE 3******************************************************************************************************-->
<table width=100% cellspacing="5" id="address_of_operator">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Address-of Operator(&)</u></h1>
<p>Pointers are special kind of variables which store address of another variable of <b>same type</b>.</p>
<p>The address of any variable can be obtained by putting '&' (address-of operator) before it. </p>
Type the following C code:
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="dtype">int</span> <span class="white">a = </span><span class="text">10</span><span class="white">;</span> <span class="comment">// 'a' is declared and initialized </span>
<span class="white">printf(</span> <span class="text">"</span> <span class="textmodf">%d</span> <span class="text">"</span> <span class="white">, a);</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n\n %p</span> <span class="text">"</span> <span class="white">, &a);</span> <span class="comment">
/* '&a' returns the address of variable 'a', and
it is a hexadecimal value. */</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n\n %u</span> <span class="text">"</span> <span class="white">, &a);</span> <span class="comment">
/* '&a' returns the address of variable 'a', but this time,
due to '%u',the return value is converted into 'unsigned int'. */</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Output</u></h3>
<pre>
<div class="codee"><span class="white">
10
0x7fff35b1ee34
900853300
</span>
</div>
</pre>
<p><b>0x7fff35b1ee34</b> or <b>900853300</b> is the address of variable 'a'. <b><i>(Value of address will vary every time you run the code.)</i></b></p>
<p>Pointers have huge advantage as they store address of other variables. We will discuss about them later, first we should learn how to use them.</p>
<span class="error"><h2>For our ease, in rest of our examples and codes, we will use <code>%u</code> and implicitly convert address from <code>hexadecimal</code> to <code>unsigned decimal</code> value.</h2></span>
</td>
<td class="adv"></td>
</tr>
</table>
<!-- **************************************************TABLE 4******************************************************************************************************-->
<table width=100% cellspacing="5" id="declaring_pointer">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Declaring a Pointer</u></h1>
<p>General Method: <span class="error"><b><i> <data-type> *<identifier> </i></b></span></p>
Example:
<pre>
<div class="codee">
<span class="dtype">int</span> <span class="white">*ptr1;</span>
<span class="dtype">float</span> <span class="white">*ptr2;</span>
<span class="dtype">char</span> <span class="white">*ptr3;</span>
</div>
</pre>
<p>As defined above- Pointers are special kind of variables which store address of another variable of <b>same type</b>.</p>
<p>Here <b>'ptr1'</b>; is a pointer variable of integer type. Hence it can store addresses of only integer variables. </p>
<p><b>'ptr2'</b>; is a pointer variable of float ype. Hence it can store addresses of only float variables. </p>
<p><b>'ptr3'</b>; is a pointer variable of character type. Hence it can store addresses of only character variables. </p>
</td>
<td class="adv"></td>
</tr>
</table>
<!-- **************************************************TABLE 5******************************************************************************************************-->
<table width=100% cellspacing="5" id="initializing_pointer">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Storing address in a Pointer</u></h1>
<p>Run this code. <i>(Note: the value of address will vary on every system and every-time you run code.) </i></p>
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="dtype">int</span> <span class="white">a = </span><span class="text">10</span><span class="white">;</span> <span class="comment">// 'a' is declared and initialized </span>
<span class="dtype">int</span> <span class="white">*ptr1 = NULL;</span> <span class="comment">// '*ptr1' is declared and initialized </span>
<span class="white">ptr1 = &a;</span> <span class="comment">// address of 'a' is stored in 'ptr1' </span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf"> %u</span> <span class="text">"</span> <span class="white">, &a);</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n\n %u</span> <span class="text">"</span> <span class="white">, ptr1);</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Output</u></h3>
<pre>
<div class="codee"><span class="white">
3019103324
3019103324
</span>
</div>
</pre>
</td>
<td class="adv"></td>
</tr>
</table>
<!-- **************************************************TABLE 6******************************************************************************************************-->
<table width=100% cellspacing="5" id="dereference">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>De-reference/ indirection operator (*)</u></h1>
<p>De-referencing means accessing value stored at particular address.</p>
<p>This operator has two usages, to declare a pointer and to access the data pointed by the pointer, i.e. access the value, stored in variable, whose address is with the pointer. </p>
<p>For example, in previous code, the variable <b>'a'</b> has value 10. The address of <b>'a'</b> is stored in <b>'ptr1'</b>. </p>
<p>
So <code>printf("%u", ptr1)</code>, will print the address of <b>'a'</b>.
<br/>
And, <code>printf("%d", *ptr1)</code>, will print the value stored at that address. So, it will print <b>'10'</b>.
</p>
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="dtype">int</span> <span class="white">a = </span><span class="text">10</span><span class="white">;</span> <span class="comment">// 'a' is declared and initialized </span>
<span class="dtype">int</span> <span class="white">*ptr1 = NULL;</span> <span class="comment">// '*ptr1' is declared and initialized</span>
<span class="white">ptr1 = &a;</span> <span class="comment">// address of 'a' is stored in 'ptr1' </span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf"> %d</span> <span class="text">"</span> <span class="white">, a);</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span> <span class="white">, ptr1);</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span> <span class="white">, *ptr1);</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Output</u></h3>
<pre>
<div class="codee"><span class="white">
10
3019103549
10
</span>
</div>
</pre>
</td>
<td class="adv"></td>
</tr>
</table>
<!-- **************************************************TABLE 7******************************************************************************************************-->
<table width=100% cellspacing="5" id="double_pointer">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Pointer pointing to a pointer</br>OR</br> Pointer to pointer</br>OR</br>Double pointer/ Triple pointer</u></h1>
<p>Pointer to a pointer means, "A pointer which stores the address of another pointer".</p>
<p>This may sound confusing, but take it in simple way. The concept of pointers is to store address of variables of same data type.</p>
<p>As we defined, "Pointers are special kind of variables which store address of another variable of same type". But, Pointers are also variables. Though the value, inside them is an address, but this value can be changed. </p>
<p><b>For example:</b></p>
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="dtype">int</span> <span class="white">a = </span><span class="text">10</span><span class="white">, b = </span><span class="text">37</span><span class="white">;</span> <span class="comment">// 'a' and 'b' are declared and initialized </span>
<span class="dtype">int</span> <span class="white">*ptr1 = NULL;</span> <span class="comment">// '*ptr1' is declared and initialized</span>
<span class="white">ptr1 = &a;</span> <span class="comment">// address of 'a' is stored in 'ptr1' </span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf"> %d</span> <span class="text">"</span> <span class="white">, a);</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span> <span class="white">, ptr1);</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span> <span class="white">, *ptr1);</span>
<span class="white">ptr1 = &b;</span> <span class="comment">// address of 'b' is stored in 'ptr1' </span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %d</span> <span class="text">"</span> <span class="white">, b);</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span> <span class="white">, ptr1);</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span> <span class="white">, *ptr1);</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Output</u></h3>
<pre>
<div class="codee"><span class="white">
10
1351588296
10
37
1351588300
37
</span>
</div>
</pre>
<p>Coming back to the topic.</p>
<p>We understood the theoretical concept of pointer-to-pointer, now we will use this concept to implement double, triple pointer and so on. </p>
<p>Let's have some variables. </p>
<pre>
<div class="codee">
<span class="dtype">int</span> <span class="white">c = </span><span class="text">45</span><span class="white">;</span>
<span class="dtype">int</span> <span class="white">*ptr1 = &c;</span>
<span class="dtype">int</span> <span class="white">**ptr2 = &ptr1;</span>
<span class="dtype">int</span> <span class="white">***ptr3 = &ptr2;</span>
</div>
</pre>
<i>
<b><u>Note:</u></b>
<ul>
<li>Single-pointer-variable can save the address of a variable. </li>
<li>Double-pointer-variable can save the address of a single-pointer-variable. </li>
<li>Triple-pointer-variable can save the address of a double-pointer-variable. And so on..</li>
</ul>
</br> Not doing so will show error for "type-mismatch", until the data-type is changed explicitly.
<p>This is because, single-pointer: it has to point a data.</p>
<p>Double-pointer: it has to point a pointer, which in points a data.</p>
<p>Triple-pointer: it has to point a pointer, which again points another pointer, which points a data</p>
And this chain extends and goes same for multi-dimensional pointers.
</i>
</br></br></br></br>
<p>The de-reference/ indirection operator (*): to understand things more clearly, interpret it as:</p>
<p><b>'*' - go to the address and access the value.</b> </p>
<center><div>
<table width=100% cellpadding="4" border="2" bordercolor="#000000" cellspacing="0" style="font-family: Arial Black, Gadget, sans-serif; font-style: normal; font-size: 1em; font-variant: normal; font-weight: normal;">
<!--************************(c)**************************-->
<tr>
<td><b>printf(" \n%u ", c);</b></td>
<td><b>Output: </b>45</td>
</tr>
<!--******************(ptr1)*******************************-->
<tr>
<td><b>printf(" \n%u ", ptr1);</b></td>
<td>Prints the value stored in 'ptr1', i.e. address of 'c'</td>
</tr>
<!--***************(*ptr1)***************************-->
<tr>
<td><b>printf(" \n%u ", *ptr1);</b></td>
<td>
<ol type="1">
<li><b>*ptr1 -> goes to the address.</b></br>Thus, goes to location of 'c'</li>
</br><li><b>Now access the value stored in it.</b></br>Thus, prints 45.</li>
</ol>
</td>
</tr>
<!--**************(ptr2)*****************************-->
<tr>
<td><b>printf(" \n%u ", ptr2);</b></td>
<td>Prints the value stored in 'ptr2', i.e. address of 'ptr1'</td>
</tr>
<!--*************(*ptr2)************************-->
<tr>
<td><b>printf(" \n%u ", *ptr2);</b></td>
<td>
<ol type="1">
<li><b>*ptr2 -> goes to the address.</b></br>Thus, goes to location of 'ptr1'</li>
</br><li><b>Now access the value stored in it.</b></br>Thus, prints the address of 'c'.</li>
</ol>
</td>
</tr>
<!--****************(**ptr2)********************-->
<tr>
<td><b>printf(" \n%u ", **ptr2);</b></td>
<td>
<p>When two operators of same precedence are used, they are evaluated according to their associativity.</p>
<p>Since the dereference/indirection operator has associativity from right to left, <b>**ptr2</b> is evaluated as <b>*(*ptr2)</b>. </p>
So,
<ol type="1">
<li><b>*ptr2 -> goes to the address.</b></br>Thus, goes to location of 'ptr1'</li>
</br><li><b>Now access the value stored in it.</b>
</br>Thus, got the address of 'c'.
</br>Thus, <b>*ptr2</b> = <b>ptr1</b>.
</br>Thus, <b>**ptr2</b> = <b>*(*ptr2)</b> = <b>*(ptr1)</b></li>
</br><li><b>*ptr1 -> goes to the address.</b></br>Thus, goes to location of 'c'</li>
</br><li><b>Now access the value stored in it.</b></br>Thus, prints 45.</li>
</ol>
</td>
</tr>
<!--*****************(ptr3)******************-->
<tr>
<td><b>printf(" \n%u ", ptr3);</b></td>
<td>Prints the value stored in 'ptr2', i.e. address of 'ptr2'</td>
</tr>
<!--************(*ptr3)*******************-->
<tr>
<td><b>printf(" \n%u ", *ptr3);</b></td>
<td>
<ol type="1">
<li><b>*ptr3 -> goes to the address.</b></br>Thus, goes to location of 'ptr2'</li>
</br><li><b>Now access the value stored in it.</b></br>Thus, prints the address of 'ptr1'.</li>
</ol>
</td>
</tr>
<!--**********(**ptr3)*******************-->
<tr>
<td><b>printf(" \n%u ", **ptr3);</b></td>
<td>
<b>**ptr3</b> is evaluated as <b>*(*ptr3)</b>.
So,
<ol type="1">
<li><b>*ptr3 -> goes to the address.</b></br>Thus, goes to location of 'ptr2'</li>
</br><li><b>Now access the value stored in it.</b>
</br>Thus, got the address of 'ptr1'.
</br>Thus, <b>*ptr3</b> = <b>ptr2</b>.
</br>Thus, <b>**ptr3</b> = <b>*(*ptr3)</b> = <b>*(ptr2)</b></li>
</br><li><b>*ptr2 -> goes to the address.</b></br>Thus, goes to location of 'ptr1'</li>
</br><li><b>Now access the value stored in it.</b></br>Thus, prints the address 'c'.</li>
</ol>
</td>
</tr>
<!--**************(***ptr3)*************-->
<tr>
<td><b>printf(" \n%u ", ***ptr3);</b></td>
<td>
<b>***ptr3</b> is evaluated as <b>*(*(*ptr3))</b>.
So,
<ol type="1">
<li><b>*ptr3 -> goes to the address.</b></br>Thus, goes to location of 'ptr2'</li>
</br><li><b>Now access the value stored in it.</b>
</br>Thus, got the address of 'ptr1'.
</br>Thus, <b>*ptr3</b> = <b>ptr2</b>.
</br>Thus, <b>**ptr3</b> = <b>*(*ptr3)</b> = <b>*(ptr2)</b></li>
</br><li><b>*ptr2 -> goes to the address.</b></br>Thus, goes to location of 'ptr1'</li>
</br><li><b>Now access the value stored in it.</b>
</br>Thus, got the address 'c'.
</br>Thus, <b>*ptr2</b> = <b>ptr1</b>.
</br>Thus, <b>***ptr3</b> = <b>*(*(*ptr3))</b> = <b>*(*(ptr2))</b> = <b>*ptr1</b> </li>
</br><li><b>*ptr1 -> goes to the address.</b></br>Thus, goes to location of 'c'</li>
</br><li><b>Now access the value stored in it.</b></br>Thus, prints 45.</li>
</ol>
</td>
</tr>
</table>
</div></center>
</td>
<td class="adv"></td>
</tr>
</table>
<!-- **************************************************TABLE 8******************************************************************************************************-->
<table width=100% cellspacing="5" id="size_of_pointer">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Size of Pointer</u></h1>
<p>Since whole and soul purpose of pointers is to store address, they must contain an integral value. Hence their size is equal to that of <b>'int'</b> or <b>'long'</b>, this depends on system and compiler. On my system <a href="#intro"><i>(specs given in introduction)</i></a>, it is equal to <b>'long'</b>.</p>
<pre>
<div class="codee">
<span class="dtype">int</span> <span class="white">*ptr1;</span>
<span class="dtype">float</span> <span class="white">*ptr2;</span>
<span class="dtype">char</span> <span class="white">*ptr3;</span>
</div>
</pre>
<p><b>char*, float*, int*,</b> all are of same size and contain integral value. Though they are defined specifically as <b>'char'</b>, <b>'float'</b>, <b>'int'</b> etc. This is done so that compiler should know that what kind of data it is going to point to. So, it becomes easy for it to de-reference it. This is all done to handle data properly.
Let's look at the code:
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf"> %u</span> <span class="text">"</span> <span class="white">, sizeof(</span><span class="dtype"> int* </span><span class="white">));</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span> <span class="white">, sizeof(</span><span class="dtype"> char* </span><span class="white">));</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span> <span class="white">, sizeof(</span><span class="dtype"> float* </span><span class="white">));</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Output</u></h3>
<pre>
<div class="codee"><span class="white">
8
8
8
</span>
</div>
</pre>
</td>
<td class="adv"></td>
</tr>
</table>
<!-- **************************************************TABLE 9******************************************************************************************************-->
<table width=100% cellspacing="5" id="pointer_to_array">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Pointer pointing to an array</u></h1>
<p>In normal conditions too, arrays are implemented using concept, similar to pointers.</p>
<pre><div class="codee"><span class="dtype">
int</span> <span class="white">arr[</span><span class="text"> 5 </span><span class="white">];
</span></div> </pre>
<p>Here the 5-consecutive memory-blocks for 'int' type are allotted and address of 1<sup>st</sup> element is stored in variable 'arr'. Accessing first element will give access to other elements as well because their memory addresses are in consecutive order.</p>
<p>Let us learn through various codes:</p>
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="dtype">int</span> <span class="white">arr[</span><span class="text"> 5 </span><span class="white">];</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf"> %u</span> <span class="text">"</span><span class="white">, arr);</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span><span class="white">, &arr[</span><span class="text"> 0 </span><span class="white">]);</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Output</u></h3>
<pre>
<div class="codee"><span class="white">
4146104064
4146104064
</span>
</div>
</pre>
<p>Both are identical. Hence it is clear that identifier of array stores the address of 1<sup>st</sup> element of the array. </p>
<p>Thus, if we store the address, stored in 'arr', into a pointer, it should also work well. Let's try: </p>
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="dtype">int</span> <span class="white">arr[</span><span class="text"> 5 </span><span class="white">] = {</span><span class="text">3</span><span class="white">,</span></span><span class="text">43</span><span class="white">,</span></span><span class="text">23</span><span class="white">,</span></span><span class="text">4</span><span class="white">,</span></span><span class="text">2</span><span class="white">};</span>
<span class="dtype">int</span> <span class="white">*b = NULL;</span>
<span class="white">b = arr;</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf"> %u</span> <span class="text">"</span><span class="white">, *b);</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n\n %u</span> <span class="text">"</span><span class="white">, arr[</span><span class="text"> 0 </span><span class="white">]);</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Output</u></h3>
<pre>
<div class="codee"><span class="white">
3
3
</span>
</div>
</pre>
<p><b><u>Note:</u></b> We didn't used '&' while initializing pointer 'b'. It is because 'arr' itself is storing address of 1<sup>st</sup> element of the array.</p>
<p>Hence, <b>'arr'</b> is <b>equivalent</b> to <b>'b'</b> but <b>NOT</b> equal to <b>'b'</b>. </p>
</br></br><div><span class="error"><h2><u>This code does't works:</u></h2></span></div>
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="dtype">int</span> <span class="white">arr[</span><span class="text"> 5 </span><span class="white">] = {</span><span class="text">3</span><span class="white">,</span></span><span class="text">43</span><span class="white">,</span></span><span class="text">23</span><span class="white">,</span></span><span class="text">4</span><span class="white">,</span></span><span class="text">2</span><span class="white">}, c = </span><span class="text">10</span><span class="white">;</span>
<span class="white">arr = &c;</span>
<span class="white">printf(</span> <span class="text">"</span> <span class="textmodf">\n%u</span> <span class="text">"</span><span class="white">, arr);</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Compilation Error:</u></h3>
<pre>
<div class="codee"><span class="error">
trial.c: In function 'main':
trial.c:6:7: error: assignment to expression with array type
arr = &c;
^
trial.c:8:11: warning: format '%u' expects argument of type
'unsigned int', but argument 2 has type 'int *' [-Wformat=]
printf( " \n%u ", arr);
^
</span>
</div>
</pre>
<p>As said, <b>'arr'</b> is <b>equivalent</b> to pointer <b>'b'</b> <i>(here, 'b' is referred to the pointer vaiable declared in previous working example.)</i>, but <b>NOT</b> equal to pointer <b>'b'</b>. As once allotted, the address stored in <b>'arr'</b> cannot be changed. </p>
</td>
<td class="adv"></td>
</tr>
</table>
<!-- **************************************************TABLE 10******************************************************************************************************-->
<table width=100% cellspacing="5" id="pointer++">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Arithmetic operations on Pointers</u></h1>
Consider this code:
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="dtype">int</span> <span class="white">i, arr[</span><span class="text"> 5 </span><span class="white">] = {</span><span class="text">3</span><span class="white">,</span></span><span class="text">43</span><span class="white">,</span></span><span class="text">23</span><span class="white">,</span></span><span class="text">4</span><span class="white">,</span></span><span class="text">2</span><span class="white">};</span>
<span class="dtype">int</span> <span class="white">*b = NULL;</span>
<span class="white">b = arr;</span>
<span class="loop">for</span><span class="white">(i = </span> <span class="text">0</span> <span class="white">; i < </span> <span class="text">5</span><span class="white">; i++)</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span><span class="white">, *(b+i));</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Output</u></h3>
<pre>
<div class="codee"><span class="white">
3
43
23
4
2
</span>
</div>
</pre>
<p><h3>Confused?</h3></p>
<p>Now it is obvious to think that there is something wrong. If the hypothetical address of first element of 'arr' is 8542, you will think: </p>
<b>
<p>b = 8542 *b = 3</p>
<p>b + i = 8543 *b = <some-garbage-value></p>
</b>
<p>Right??</p>
<p>Wrong. Now this is where declaring the data-type of pointer comes into play.</p>
<p>Basically:</p>
<p><b>b + 1 ≠ 8543</b></p>
<p>The compiler knows <b>'b'</b> is a pointer <b>and</b> it also knows that <b>'b'</b> is an integer pointer. Hence it knows that integers are consist of 4 consecutive bytes (size of <code>int </code> may change from compiler to compiler and machine to machine). Thus: </p>
<p><b>(b + 1) = 8542 + sizeof(int)</b></p>
<p>In General: <span class="error"><b><i> <pointer + 1> = <address-value-inside-pointer + sizeof(data-type-of-pointer)> </i></b></span></p>
<p>In Linux, 64 bit, gcc/g++ compiler, size of <code>int</code> is 4 bytes.</p>
<div>
<table width=100% border="2" bordercolor="black" cellpadding="4" cellspacing="0"style="font-family: Arial Black, Gadget, sans-serif; font-style: normal; font-size: 1em; font-variant: normal; font-weight: bold;">
<b>
<tr>
<td> i = 0</td>
<td> (b + i)</td>
<td> (b + 0)</td>
<td> address of arr[0]</td>
<td> *(b + 0)</td>
<td> 3</td>
</tr>
<tr>
<td> i = 1</td>
<td> (b + i)</td>
<td> (b + 1)</td>
<td> address of arr[1]</td>
<td> *(b + 1)</td>
<td> 43</td>
</tr>
<tr>
<td> i = 2</td>
<td> (b + i)</td>
<td> (b + 2)</td>
<td> address of arr[2]</td>
<td> *(b + 2)</td>
<td> 23</td>
</tr>
<tr>
<td> i = 3</td>
<td> (b + i)</td>
<td> (b + 3)</td>
<td> address of arr[3]</td>
<td> *(b + 3)</td>
<td> 4</td>
</tr>
<tr>
<td> i = 4</td>
<td> (b + i)</td>
<td> (b + 4)</td>
<td> address of arr[4]</td>
<td> *(b + 4)</td>
<td> 2</td>
</tr>
</b>
</table>
</div>
</td>
<td class="adv"></td>
</tr>
</table>
<!-- **************************************************TABLE 11******************************************************************************************************-->
<table width=100% cellspacing="5" id="more">
<!--*********************ROW 1*************************************-->
<tr>
<td class="adv"></td>
<td class="main">
<h1 align="center"><u>Some more Results</u></h1>
<p><h2>This code works:</h2></p>
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="dtype">int</span> <span class="white">i, arr[</span><span class="text"> 5 </span><span class="white">] = {</span><span class="text">3</span><span class="white">,</span></span><span class="text">43</span><span class="white">,</span></span><span class="text">23</span><span class="white">,</span></span><span class="text">4</span><span class="white">,</span></span><span class="text">2</span><span class="white">};</span>
<span class="dtype">int</span> <span class="white">*b = NULL;</span>
<span class="white">b = arr;</span>
<span class="loop">for</span><span class="white">(i = </span> <span class="text">0</span> <span class="white">; i < </span> <span class="text">5</span><span class="white">; i++)</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span><span class="white">, b[i]);</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Output</u></h3>
<pre>
<div class="codee"><span class="white">
3
43
23
4
2
</span>
</div>
</pre>
<p>Though <b>'b'</b> is a pointer, but above results conclude: </p>
<div>
<table width=100% border="2" bordercolor="black" cellpadding="4" cellspacing="0"style="font-family: Arial Black, Gadget, sans-serif; font-style: normal; font-size: 1em; font-variant: normal; font-weight: bold;">
<b>
<tr>
<td> b[0]</td>
<td> is treated same as</td>
<td> *(b + 0)</td>
</tr>
<tr>
<td> b[1]</td>
<td> is treated same as</td>
<td> *(b + 1)</td>
</tr>
<tr>
<td> b[2]</td>
<td> is treated same as</td>
<td> *(b + 2)</td>
</tr>
<tr>
<td> b[3]</td>
<td> is treated same as</td>
<td> *(b + 3)</td>
</tr>
<tr>
<td> b[4]</td>
<td> is treated same as</td>
<td> *(b + 4)</td>
</tr>
</b>
</table>
<div>
<p>What will happen if we prefix <b>&i</b> with dereference operator?</p>
<p><b>&</b> or address-of operator returns the address of the variable to which it is added as prefix. And the work of dereferenc/ indirection operator is to <b>go to the address and access the value.</b></p>
<p>So, if we prefix <b>&i</b> with dereference/ indirection operator, then <b>*</b> will nullify the effect of address-of (<b>&</b>) operator.</p>
<div>
<table width=100% border="2" bordercolor="black" cellpadding="4" cellspacing="0"style="font-family: Arial Black, Gadget, sans-serif; font-style: normal; font-size: 1em; font-variant: normal; font-weight: bold;">
<b>
<tr>
<td colspan="2"><font color="red"> *</font>(<font color="red">&</font>i)</td>
<td colspan="2"> is same as</td>
<td colspan="3"> i </td>
</tr>
<tr>
<td colspan="7"><font color="white">t</font></td>
</tr>
<tr>
<td> <font color="red">*</font>(<font color="red">&</font>b[i])</td>
<td> is same as</td>
<td> <font color="red">*</font>(<font color="red">&</font>*(b + i))</td>
<td> is same as</td>
<td> *(b + i) </td>
<td> is same as</td>
<td> b[i]</td>
</tr>
</b>
</table>
<div>
</br>
</br>
</br>
</br>
<p>Since <b>b</b> is a pointer and *(b + i) works same as b[i].</p>
<p>Thus, *(a + i) should also work. as 'arr' is equivalent to a pointer.</p>
<h2>This code works:</h2>
<pre>
<div class="codee">
<span class="preprocess">#include</span> <span class="text"><stdio.h></span>
<span class="dtype">int</span> <span class="white"> main()</span>
<span class="white">{</span>
<span class="dtype">int</span> <span class="white">i, arr[</span><span class="text"> 5 </span><span class="white">] = {</span><span class="text">3</span><span class="white">,</span></span><span class="text">43</span><span class="white">,</span></span><span class="text">23</span><span class="white">,</span></span><span class="text">4</span><span class="white">,</span></span><span class="text">2</span><span class="white">};</span>
<span class="dtype">int</span> <span class="white">*b = NULL;</span>
<span class="white">b = arr;</span>
<span class="loop">for</span><span class="white">(i = </span> <span class="text">0</span> <span class="white">; i < </span> <span class="text">5</span><span class="white">; i++)</span>
<span class="white">printf(</span> <span class="text">"</span><span class="textmodf">\n %u</span> <span class="text">"</span><span class="white">, *(arr + i));</span>
<span class="white">return 0;</span>
<span class="white">}</span>
</div>
</pre>
<h3><u>Output</u></h3>
<pre>
<div class="codee"><span class="white">
3
43