-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.html
More file actions
390 lines (332 loc) · 15 KB
/
pointer.html
File metadata and controls
390 lines (332 loc) · 15 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
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<title>CodingElite</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark navm">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">CodingElite</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link " aria-current="page" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link " href="/codingelite.org/blog.html">Blog</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link active dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
C Tutorials
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="/codingelite.org/introduction.html">introduction</a></li>
<li><a class="dropdown-item" href="/codingelite.org/branching.html">Branching Statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/looping.html">looping statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/function.html">Function</a></li>
<li><a class="dropdown-item" href="/codingelite.org/array.html">Array</a></li>
<li><a class="dropdown-item" href="/codingelite.org/string.html">string</a></li>
<li><a class="dropdown-item" href="/codingelite.org/pointer.html">pointers</a></li>
<li><a class="dropdown-item" href="/codingelite.org/structure.html">structures</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="/codingelite.org/blog.html">More Topics</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/book.html">Books</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/contact.html">Contact us</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="bg-light">
<div class="container bg-c">
<h1 class="hed hedi-h1"><strong>Pointer</strong></h1>
<h3>Pointer in c</h3>
<p>Pointers are used to access the variables directly by using their address Locations. The value has been
assigned to the variable gets stored in that particular memory location. </p>
<ul>
<li>
<h5><b>Null pointer</b></h5>
</li>
<p>A pointer that is assigned NULL is called a "null pointer".The value of null pointer is 0.</p>
<li>
<h5><b>Void pointer </b></h5>
</li>
<p>A void pointer is also called as generic pointer. It doesn't have any standard data type. In this pointer
void keyword is used. It is used to store the address of any variable.</p>
<h6>Syntax:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">void *p = NULL; //Void pointer </pre>
</div>
</ul>
<h4>Pointer operators</h4>
<p>Two types of pointer operators:</p>
<ol>
<li>address operator[&]</li>
<li>indirection/derefrencing operator[*]</li>
</ol>
<h5>1.address operator[&]</h5>
<p> & is used to determine the address of the variable.</p>
<h6>Example:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">int a=1;
int *aptr;
aptr= &a; //aptr gets address of a </pre>
</div>
</div>
<h5>2.indirection/derefrencing operator[*]</h5>
<p>* can be used to assign a value to a location in memory.</p>
<h6>Example:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">*aptr=2; //changes a to 2 </pre>
</div>
</div>
<h4>Declaration of pointer</h4>
<p>It contain data type and pointer name with astric sign.</p>
<h6>Syntax:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">data type *pointer_name;</pre>
</div>
</div>
<p>here, * is pointer variable. <br> <br>Example :</p>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">int *ptr;</pre>
</div>
</div>
<p>This declares that the pointer variable points to an integer data type.</p>
<h4>Initialization of pointer</h4>
<p>Initialization of pointer variable is done by assigning to it the address of a variable</p>
<h6>Syntax:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">pointer_name= &variable;</pre>
</div>
</div>
<h6>Example:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl"> int a=2081;
int *ptr; //Declaration of pointer
ptr = &a; //Initialization of pointer </pre>
</div>
</div>
<h4>Advantages of pointer</h4>
<ol>
<li>Dynamic memory Allocation</li>
<li>Pointer provide direct access to memory.</li>
<li>Reduce the execution time of the program.</li>
<li>It provide an alternate way to access array elements.</li>
<li>Using pointers, arrays and structures can be handled in more efficient way.</li>
<li>Searchin of large size data is very efficient.</li>
</ol>
<h4>Disadvantages of pointrer</h4>
<ol>
<li>Pointers are slower than normal variables.</li>
<li>Poiner lead to memory leaks.</li>
<li>Pointer are a litle complex to understand.</li>
<li>If pointers are updated with incorrect values, it might lead to memory corruption.</li>
</ol>
<h4>Methods of parameter passing</h4>
<ul>
<li><h5>Call by value</h5></li>
<p> [It is completed in function tutorial.]</p>
<li><h5>Call by reference</h5> </li>
<p>It is a method of passing arguments to a function copies the address of an actual parameter into the formal parameter. <br>
To pass value by reference, pointer is passed as argument to function call like any other value. </p>
<h6>Example:</h6>
<div class="row">
<div class="col-md-8">
<pre class="pre-bl">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
</span>
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
/* calling a function to swap the values */
swap(&a, &b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
void swap(int *a, int *b) {
int temp;
temp = a; / save the value of a */
a = *b; / put y into a */
b = temp; / put temp into b */
return;
}
</pre>
<p><b>Output</b></p>
<pre class="pre-bl">
Before swap, value of a : 100
Before swap, value of b : 200
After swap, value of a : 200
After swap, value of b : 100
</pre>
</ol>
</div>
</div>
</ul>
<h4><b>Sample programs:</b></h4>
<h6>1.C program to addition of two number using pointer</h6>
<div class="col-md-8">
<pre class="pre-bl">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
</span>
int main()
{
int first, second, *p, *q, sum;
printf("Enter any two integers\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of the two numbers = %d\n", sum);
return 0;
}
</pre>
<p><b>Output</b></p>
<pre class="pre-bl">
Enter any two integers
2
3
Sum of the two numbers = 5
</pre>
</div>
<h6>2.C Program to reverse a number using pointer </h6>
<div class="col-md-8">
<pre class="pre-bl">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
</span>
int main( )
{
int num, rem, rev=0 ;
int *pn, *pr ;
printf(" Enter the number \n") ;
scanf("%f ",& num) ;
pn = & num ;
pr = & rev ;
do
{
rem = ( *pn ) % 10 ;
*pr = ( *pr * 10 ) + rem ;
*pn = ( *pn ) / 10 ;
}
while( *pn > 0) ;
printf("\n Reverse of Number is : %d ",*pr) ;
return ( 0 );
}
</pre>
<p><b>Output</b></p>
<pre class="pre-bl">
Enter the number
123
Reverse of Number is : 321
</pre>
</div>
<h6>3.C program to find the largest number using pointer </h6>
<div class="col-md-8">
<pre class="pre-bl">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int x,y, *large, *xptr,*yptr;
clrscr();
printf("Enter the value of x and y \n");
scanf("%d%d",&x,&y);
xptr=&x;
yptr=&y;
if(*xptr>*yptr)
{
large=xptr;
}
else
{
large=yptr;
}
printf("The largest number is : %d",*large);
getch();
}
</pre>
<p><b>Output</b></p>
<pre class="pre-bl">
Enter the value of x and y
34
45
The largest number is : 45
</pre>
</div>
<h5>Examples For Practice</h5>
<ol>
<li>Find reverse of the string using pointer.</li>
<li>Write a program to print the elements of a 1D integer array in reverse order along with memory address of each element using pointer. <br>(use decrement operator on pointer to print the address)
<br> e.g for array containing 10,20,30…output will be
<br> 30 at 65528……..
<br> 20 at 65526
<br> 10 at 65524
</li>
<li>5) Write a program to print characters in string along with memory addresses of each character
<br> e.g for sting : “hello” output will be
<br> h at 65524
<br> e at 65525……..
</li>
</ol>
<a href="/codingelite,org/string.html"> <button type="button" class="btn btn-primary btn-m"> <<Previous</button> </a>
<a href="/codingelite.org/structure.html"> <button type="button" class="btn btn-primary btn-m btn-r">Next >></button></a>
</div>
</div>
<footer class="page-footer font-small blue navbar-dark bg-light footer-ptt">
<div class="footer-copyright text-center py-3">
© Copyright 2021. All Right Reserved. | Designed by the <a href="/codingelite.org/about.html">CodingElite team</a>. |
<a class="color-b" href="/codingelite.org/privacyPolicy.html">Privacy policy</a> | <a class="color-b" href="/codingelite.org/contact.html">Contact us</a>
</div>
</footer>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js" integrity="sha384-q2kxQ16AaE6UbzuKqyBE9/u/KzioAlnx2maXQHiDX9d4/zp8Ok3f+M7DPm+Ib6IU" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.min.js" integrity="sha384-pQQkAEnwaBkjpqZ8RU1fF1AKtTcHJwFl3pblpTlHXybJjHpMYo79HY3hIi4NKxyj" crossorigin="anonymous"></script>
-->
</body>
</html>