-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNullPointers.cpp
More file actions
32 lines (24 loc) · 1.01 KB
/
NullPointers.cpp
File metadata and controls
32 lines (24 loc) · 1.01 KB
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
#include <iostream>
int main()
{
// Null value = a special value that means something has no value.
// when a pointer is holding a null value,
// it means that the pointer is not pointing to anything
// nullptr = keyword that represents a null pointer literal
// nullptrs are helpful when determining if an address was successfully assigned to a pointer
//when using ptrs, be careful that your code is not derecferencing a null pointer or
//pointing to a free memory. This will cause undefined behavior (Segmentation fault)
int *pointer = nullptr; // pointer is initialized to null
int x = 123;
pointer = &x; // pointer is assigned the address of x
if(pointer == nullptr)
{
std::cout << "address was not assigned\n"; // pointer is null
}
else
{
std::cout << "address was assigned\n"; // pointer is not null
std::cout << *pointer << '\n'; // prints the variable stored at the memeory address
}
return 0;
}