-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointers_lesson4.cpp
42 lines (30 loc) · 1.14 KB
/
pointers_lesson4.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
//Note-1:
/*Assigning the address of a variable to a pointer using unary operator (&)
which returns the address of that variable.
Accessing the value stored in the address using unary operator (*)
which returns the value of the variable located at the address specified by its operand.
*/
/* this is an integer variable with value = 54 */
int a = 54;
// this is a pointer that holds the address of the variable 'a'.
// if 'a' was a float, rather than int, so should be its pointer.
int * pointerToA = &a;
// If we were to print pointerToA, we'd obtain the address of 'a':
std::cout << "pointerToA stores " << pointerToA << '\n';
// If we want to know what is stored in this address, we can dereference pointerToA:
std::cout << "pointerToA points to " << * pointerToA << '\n';
//Note-2
//C++ provides two-pointer operators, which are Address of Operator (&) and Indirection Operator (*).
#include<iostream>
#include<string>
int main ()
{
int * pointerI;
int number;
pointerI = &number;
*pointerI = 45;
std::cout << "number = "<<number<<"\n";
return 0;
}
//number = 45
//useful content: https://www.youtube.com/watch?v=rtgwvkaYt1A