-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.c
More file actions
138 lines (117 loc) · 2.44 KB
/
pointer.c
File metadata and controls
138 lines (117 loc) · 2.44 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
// ================================================
// * Language: C
// * Topic: Pointer
// =================================================
// pointer example 1️⃣
#include <stdio.h>
int main(){
int age = 20 ;
int *ptr = &age;
int _age = *ptr ;
printf(" Output is %d\n" , _age);
return 0;
}
// Print Address
#include <stdio.h>
int main()
{
int age = 10;
int *ptr = &age;
int _age = *ptr;
//printf("Output is %p\n" ,&age );
printf("Output is %u\n", &ptr); // Different
printf("Output is %u\n", ptr);
printf("Output is %u\n", &age);
return 0;
}
// Print value
#include <stdio.h>
int main(){
int age = 12 ;
int *ptr = &age ;
int _age = *ptr;
printf("Output is : %d\n", age);
printf("Output is : %d\n" , *ptr);
printf("Output is : %d\n" , *(&age)) ;
return 0;
}
// ================================================
// * Language: C
// * Topic: Pointer to Pointer
// =================================================
// pointer example 2️⃣
#include <stdio.h>
int main()
{
int price = 100;
int *ptr = &price;
int **pptr = &ptr;
return 0;
}
// 3️⃣ Call by value
#include <stdio.h>
void num(int n);
int main(){
int number = 10;
num(number);
printf("Number is %d\n" , number );
return 0;
}
void num(int n){
n = n*n;
printf("Square is %d\n " , n);
}
// 4️⃣ Call by Reference
#include <stdio.h>
void _square(int *n);
void square(int a);
int main()
{
int x = 4;
// int y = 3;
square(x); // function call
printf("value of x %d\n", x);
_square(&x);
printf("value of &x %d\n", x);
return 0;
}
// fuction definition
void square(int a)
{
// a = a*a;
int result = a * a;
printf(" Result of square %d\n", result);
}
void _square(int *n)
{
*n = (*n) * (*n);
printf("_square is %d\n", *n);
}
// 👉👉 🔹🔹Qexample - 5️⃣ Swap 2 Numbers a & b
#include <stdio.h>
int main(){
int a = 55 ;
int b = 33;
int t = a;
a = b ;
b = t;
printf("a = %d & b = %d\n" , a ,b);
return 0;
}
// 👉👉 🔹🔹example - 6️⃣ Swap 2 Numbers a & b call by reference
#include <stdio.h>
int _swap(int *a ,int *b);
int main(){
int a = 10;
int b = 20;
_swap(&a ,&b);
printf("a = %d b = %d\n" ,a,b );
return 0 ;
}
// Fucntion difinition;
int _swap(int *a , int *b){
int t= *a;
*a = *b;
*b = t;
printf("a = %d & b = %d\n", *a,*b);
}