address-opereator

This is for the readers who have basic c++ background. Those days I have noticed that there’s char ‘&’ in front of function. So decided to record it.

I suggest an online c++ compiler : (You can test your simple programs on it.)

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
#include <iostream>
#include <memory>
using namespace std;


int& fun(int &a)
{
cout << "aa " << &a <<endl;
return a;
}
int main()
{
int b;
cout << "b: " << &b <<endl;

b = 4;
cout << "fun: "<< &fun(b) << endl;

int a;
cout << "a: " << &a << endl;

a = fun(b);
cout << a << endl;
cout << &a << endl;
}

The result is :

1
2
3
4
5
6
7
b: 0x7ffe5ec356b8
fun: aa 0x7ffe5ec356b8
0x7ffe5ec356b8
a: 0x7ffe5ec356bc
aa 0x7ffe5ec356b8
4
0x7ffe5ec356bc

To be honest, & and * are too hard in c++.


Your Majesty, coding and blogging is hard, but a cup of tea/coffee would be the best reward. Scan the QRcode below, I’ll swear my allegiance to you. :) memeda

Author

Keneyr

Posted on

2020-03-29

Updated on

2020-03-29

Licensed under

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×