-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11_pointers.cpp
78 lines (60 loc) · 1.43 KB
/
11_pointers.cpp
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
#include<iostream>
using namespace std;
/*
int main(){
int a=10;
int *aptr=&a;
cout<<*aptr<<endl;
cout<<aptr<<endl;//address value of variable a
*aptr = 20; //change the value of variable a
cout<<*aptr<<endl;
return 0;
}
//Pointer airthmatic
int main(){
int arr[] = {10,20,30};
cout<<*arr<<endl;//will give only 0th index element
int *ptr=arr;
for(int i=0; i<3; i++){
cout<<*ptr<<endl; // by arr we can do by putting *(arr+i)
ptr++; // cannot be done using arr++ its not legal
}
return 0;
}
//Pointer to pointer
int main(){
int a=10;
int *p;
p=&a;
cout<<*p<<endl; // print value of a
int **q=&p;
cout<<*q<<endl; // will print address of variabe a
cout<<**q<<endl; //will print value of a
return 0;
}
//pointer requirement
void increment(int a){
a++;
}
int main(){
int a=2;
increment(a);
cout<<a<<endl;// give 2 not 3 thats why we require pointer to solve such problems
return 0;
}
*/
//Passing pointer to fuinction
void swap(int *a, int *b){
int temp=*a;
*a=*b;
*b=temp;
}
int main(){
int a=2;
int b=4;
int *aptr=&a;
int *bptr=&b;
swap(*aptr,*bptr);// it is call by value and call by refrence can be done by passing address instead of pointers like swap(&a,&b) and this cannot be done by using swap(a,b) because of function defination
cout<<a<<" "<<b<<endl;
return 0;
}