-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_Pointers.c
114 lines (87 loc) · 1.92 KB
/
9_Pointers.c
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
#include <stdio.h>
// void square(int n);
// void _square(int *n);
void swap(int a, int b);
void _swap(int *a,int *b);
/*Pointers:
A variable that stores the memory address of another variable
Syntax:
int age = 22;
int *ptr = &age;
*-->value at address
&-->address of variable
int _age=*ptr;
declaring pointers:
int *ptr;
char *ptr;
float *ptr;
Format specifier
printf("%p",&age);
printf("%p",ptr);
printf("%p",&ptr);
Pointer Arithmetic:
Pointer can be incremented and decremented-->
int age = 22;
int *ptr = &age;
ptr++;
*/
int main()
{
int age = 22;
int *ptr = &age;
int _age = *ptr;
// address-->address on hexadecimal format
// printf("%p\n",&age);
// unsigned int-->address on integer
/*printf("%u\n",&age);
printf("%u\n",ptr);*/
/*
// Format Specifier
//value
printf("%d\n",age);
printf("%d\n",*ptr);
printf("%d\n",*(&age));*/
/*Pointer to Pointer
A variable that stores the memory address of another pointer*/
/*Pointers in Function call
call by value: we pass value of variable as argument
call by reference: we pass address of variable as argument*/
/*int number = 4;
square(number);
printf("number = %d\n", number);
_square(&number);
printf("number = %d\n", number);*/
int x=3,y=5;
// swap(x,y);
// printf("Swapping of x & y: %d %d\n", x, y);
_swap(&x,&y);
printf("Swapping of x & y: %d %d\n", x, y);
return 0;
}
void square(int n)
{
n = n * n;
printf("square = %d\n", n);
}
// Pointers changes the value and update it,here it sets as 16
void _square(int *n)
{
*n = (*n) * (*n); // 4 * 4
printf("square = %d\n", *n);
}
// call by value
/*void swap(int a, int b)
{
int k;
k = a;
a = b;
b = k;
printf("Swapping of a & b: %d %d\n", a, b);
}*/
//call by reference
void _swap(int *a,int *b){
int t = *a;
*a=*b;
*b=t;
printf("Swapping of a & b: %d %d\n", *a, *b);
}