Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd3dcc6

Browse files
pass by address, by reference. Return by address and by reference
1 parent ab65ae4 commit fd3dcc6

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
3+
4+
using namespace std;
5+
6+
void swap(int *a, int *b)
7+
{
8+
int temp = *a;
9+
*a = *b;
10+
*b = temp;
11+
}
12+
13+
14+
15+
int main(void)
16+
{
17+
int x = 1978, y = 1977;
18+
swap(&x,&y);
19+
20+
cout<<x<<" "<<y;
21+
22+
return 0;
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
void swap(int &a, int &b)
6+
{
7+
cout<<"The address of variable a ="<<&a<<endl<<"The address of variable b ="<<&b<<endl;
8+
9+
int temp = a;
10+
a = b;
11+
b = temp;
12+
}
13+
14+
int main(void)
15+
{
16+
int x = 1953, y = 1954;
17+
cout<<"Before swaping the values, x = "<<x<<" and y = "<<y<<endl;
18+
cout<<"The address of variable x ="<<&x<<endl<<"The address of variable y ="<<&y<<endl;
19+
20+
swap(x,y);
21+
22+
cout<<"After swaping the values, x="<<x<<" and y="<<y<<endl;
23+
24+
return 0;
25+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
6+
int * myfunction()
7+
{
8+
int *p = new int[10];
9+
10+
cout<<"The memory address of the p variable is "<<p<<endl;
11+
12+
for (int i = 0; i<10; i++)
13+
{
14+
p[i] = i + 1;
15+
}
16+
17+
return p;
18+
}
19+
20+
int main(void)
21+
{
22+
int *r = myfunction();
23+
24+
cout<<"The address of the r variable is "<<r<<endl;
25+
26+
for(int i = 0; i<10; i++)
27+
{
28+
cout<<r[i]<<endl;
29+
}
30+
31+
delete[] r;
32+
33+
34+
// instantiating the elements of an array using the pointers
35+
int *m = new int[5];
36+
int *temp = m;
37+
cout<<"m is "<<m<<endl;
38+
for(int j = 0;j<5 ;j++)
39+
{
40+
*temp = j;
41+
temp++;
42+
cout<<m[j]<<endl;
43+
44+
}
45+
46+
delete[] m;
47+
m = nullptr;
48+
temp = nullptr;
49+
50+
return 0;
51+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int & fun(int &a)
6+
{
7+
cout<<"The address of a is "<<&a<<" and the value of a is "<<a<<endl;
8+
return a;
9+
}
10+
11+
int main(void)
12+
{
13+
int x = 1953;
14+
15+
fun(x) = 1954;
16+
17+
cout<<"The value of x is "<<x<<endl;
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /