1#include <iostream>
2using namespace std;
3void func (string &s)
4{
5 s+="d";
6}
7int main()
8{
9 string a="abc";
10 func(a);
11 cout << a << endl;
12 return 0;
13}
14// output will be abcd
1// C++ program to demonstrate differences between pointer
2// and reference.
3#include <iostream>
4using namespace std;
5
6struct demo
7{
8 int a;
9};
10
11int main()
12{
13 int x = 5;
14 int y = 6;
15 demo d;
16
17 int *p;
18 p = &x;
19 p = &y; // 1. Pointer reintialization allowed
20 int &r = x;
21 // &r = y; // 1. Compile Error
22 r = y; // 1. x value becomes 6
23
24 p = NULL;
25 // &r = NULL; // 2. Compile Error
26
27 p++; // 3. Points to next memory location
28 r++; // 3. x values becomes 7
29
30 cout << &p << " " << &x << endl; // 4. Different address
31 cout << &r << " " << &x << endl; // 4. Same address
32
33 demo *q = &d;
34 demo &qq = d;
35
36 q->a = 8;
37 // q.a = 8; // 5. Compile Error
38 qq.a = 8;
39 // qq->a = 8; // 5. Compile Error
40
41 cout << p << endl; // 6. Prints the address
42 cout << r << endl; // 6. Print the value of x
43
44 return 0;
45}