-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18-references.cpp
More file actions
41 lines (32 loc) · 961 Bytes
/
18-references.cpp
File metadata and controls
41 lines (32 loc) · 961 Bytes
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
#include <iostream>
#include <string>
using namespace std;
void main() {
//references can refer to existing elements to access their data
string food = "pizza";
string meal = &food; // reference to the food variable
cout << food << "\n"; //prints pizza
cout << meal << "\n"; // prints the same thing.
}
//Memory addresses
#include <iostream>
#include <string>
using namespace std;
void main() {
string food = "pizza";
cout << &food; // returns hexadecimal memory address example: 0x6dfed4.
}
//Pointers
//pointers store memory address of another variable inside a assigned variable
//Example
#include <iostream>
#include <string>
using namespace std;
void main() {
string food = "pizza";
string *ptr = &food; //assigning the address of food variable to ptr
cout << *ptr; //outputs pizza.
// we can also modify data using pointers
*ptr = "hamburger";
cout << *ptr // this will now output hamburger.
}