[Book] Mastering C/C++ Pointer
Last Update:
Word Count:
Read Time:
El libro
# Introduction
This article is used to keep notes and summaries of the book "Mastering C/C++ Pointer".
The content will be continuously updated as I read through the book.
Java and Python **DO NOT SUPPORT** pointer. However, this book introduces a similar concept——*reference*.
C# supports pointer, but developers have to enable **unsafe mode**.
# Reflection
Finally I completed this book!
Before starting this book, you should have a fundamental knowledge of C and C++. Basic knowledge such as data types, arrays, iteration, functions, and `iostream` is required.
Althrough this book covers the concept of **references** in Java, C# and Python, these are not the main focus if your goal is to study pointers.
Pointers are an essential part of C and significant part of C++.
This book also cover a bit of `operator` in C++.
Learning C without understanding pointers only shows that you have been exposed to C. On the other hand, knowing how to use pointers **DOES NOT** necessarily mean that you know C++.
Part I - C Language
Chapter.1 - Fundamental
1.1 - Variable
1 | |
Output:1
2
3
4
5
6Address of x: 0x7ffdf75f71ec
Address of y: 0x7ffdf75f71e8
Address of z: 0x7ffdf75f71e4
Value of x: 100
Value of y: 200
Value of z: 300
1.2 - 1 Dimension
1 | |
Output: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
26Enter x[0]: 10
Enter x[1]: 20
Enter x[2]: 30
Enter x[3]: 40
Enter x[4]: 50
x[0] = 10
x[1] = 20
x[2] = 30
x[3] = 40
x[4] = 50
Enter x[0]: 50
Enter x[1]: 40
Enter x[2]: 30
Enter x[3]: 20
Enter x[4]: 10
x[0] = -1653994464
x[1] = -1653994460
x[2] = -1653994456
x[3] = -1653994452
x[4] = -1653994448
x[0] = 50
x[1] = 40
x[2] = 30
x[3] = 20
x[4] = 10
1 | |
Output:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17&x[0] = 0x7fff5863ac80
&x[1] = 0x7fff5863ac84
&x[2] = 0x7fff5863ac88
&x[3] = 0x7fff5863ac8c
&x[4] = 0x7fff5863ac90
x+0 = 0x7fff5863ac80
x+1 = 0x7fff5863ac84
x+2 = 0x7fff5863ac88
x+3 = 0x7fff5863ac8c
x+4 = 0x7fff5863ac90
x[0] = 10
x[1] = 20
x[2] = 30
x[3] = 40
x[4] = 50
1.3 - 2 Dimension
1.4 - Structure
Chapter.2 - Pointer And Variable
2.1 - One Pointer: One Key
1 | |
Output:1
2
3
4
5
6
7
8
9Address of x: 0x7ffd8b7738ec, value: 100
Address of y: 0x7ffd8b7738e8, value: 200
Address of ptr: 0x7ffd8b7738e0
Content of ptr: 0x7ffd8b7738ec
x = 100, *ptr = 100
Content of ptr: 0x7ffd8b7738e8
y = 200, *ptr = 200
2.2 - Two Pointer: Two Key
1 | |
Output:1
2
3
4&x = 0x7ffd77ac28fc
&p1 = 0x7ffd77ac28f0, p1 = 0x7ffd77ac28fc
&p2 = 0x7ffd77ac28e8, p2 = 0x7ffd77ac28f0
**p2 = 100, *p1 = 100
2.3 - Three Pointer: Three Key
1 | |
Output:1
2
3
4
5
6&x = 0x7ffcdac5f87c
&p1 = 0x7ffcdac5f870, p1 = 0x7ffcdac5f87c
&p2 = 0x7ffcdac5f868, p2 = 0x7ffcdac5f870
&p3 = 0x7ffcdac5f860, p3 = 0x7ffcdac5f868
***p3 = 100, **p2 = 100, *p1 = 100
2.4 - Pointer And const
1 | |
Output:1
2
3a = 100, *p = 100
b = 200, *q = 200
*p = 200
Another case: Both actions are not allowed.1
const int * const p = &a;
Chapter.3 - Pointer and Array
3.1 - One Dimension Array
1 | |
Output: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#include <stdio.h>
int main()
{
int arr[] = {100, 101, 102};
int *ptr = arr;
int i, size = 0;
size = (sizeof(arr)/sizeof(arr[0]));
//-----------Using arr-----------\\
printf("arr pointer:\n");
for (i = 0; i < size; i++)
printf("&arr[%d] = %x\n", i, &arr[i]);
printf("\n");
for (i = 0; i < size; i++)
printf("arr+%d = %x\n", i, arr + i);
printf("\n");
for (i = 0; i < size; i++)
printf("arr[%d] = %d\n", i, arr[i]);
//-----------Using ptr-----------\\
printf("\nUsing ptr:\n");
for (i = 0; i < size; i++)
printf("*(arr+%d) = %d\n", i, ptr + i);
printf("\n");
for (i = 0; i < size; i++)
printf("ptr[%d] = %d\n", i, ptr[i]);
printf("\n");
for (i = 0; i < size; i++)
printf("*(ptr+%d) = %d\n", i, *(ptr+i));
return 0;
}
Chapter.4 - Pointer And Function
4.2 - swap
4.4 - Pointer To Function
1 | |
Output:1
2
3
4
5Enter x and y: 10 2
10 + 2 = 12
10 - 2 = 8
10 * 2 = 20
10 / 2 = 5
4.5 - Function Returns a Pointer
1 | |
Chapter.5 - Pointer And String
Chapter.6 - Pointer And Struct
Chapter.7 - Linked List
Chapter.8 - BST
Chapter.9 - Pointer And File
Part II - C++
Chapter.10 - Reference
10.1 - Variable and Reference
In C, there are only pointers; it does not have the concept of reference like C++.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#include <iostream>
using namespace std;
int main()
{
int k = 100;
int &r = k;
int *p = &k;
cout << "&k=" << &k << endl;
cout << "&r=" << &r << endl;
cout << "&p=" << &p << endl;
cout << "p=" << p << endl << endl;
cout << "k=" << k << endl;
cout << "r=" << r << endl;
cout << "*p=" << *p << endl << endl;
r++;
cout << "k=" << k << endl;
cout << "r=" << r << endl;
cout << "*p=" << *p << endl << endl;
k++;
cout << "k=" << k << endl;
cout << "r=" << r << endl;
cout << "*p=" << *p << endl << endl;
return 0;
}
Output:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16&k=0x7ffd0d5ca174
&r=0x7ffd0d5ca174
&p=0x7ffd0d5ca168
p=0x7ffd0d5ca174
k=100
r=100
*p=100
k=101
r=101
*p=101
k=102
r=102
*p=102
10.2 - swap
In C, we implement swap() with pointers. In C++, we are allowed to implement it with reference: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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65#include <iostream>
using namespace std;
void swap_by_address(int *x, int *y);
void swap_by_reference(int &, int &y);
void swap_by_value(int, int); //Just for demonstration, this method cannot do swapping!
int main()
{
int x = 100, y = 200;
//Call by address
x = 100; y = 200;
cout << "Call by value" << endl;
cout << "Before swapping..." << endl;
cout << "x=" << x << ", y=" << y << endl;
swap_by_value(x, y);
cout << "After swapping..." << endl;
cout << "x=" << x << ", y=" << y << endl << endl;
//Call by address
x = 100; y = 200;
cout << "Call by address" << endl;
cout << "Before swapping..." << endl;
cout << "x=" << x << ", y=" << y << endl;
swap_by_address(&x, &y);
cout << "After swapping..." << endl;
cout << "x=" << x << ", y=" << y << endl << endl;
//Call by reference
x = 100; y = 200;
cout << "Call by reference" << endl;
cout << "Before swapping..." << endl;
cout << "x=" << x << ", y=" << y << endl;
swap_by_reference(x, y);
cout << "After swapping..." << endl;
cout << "x=" << x << ", y=" << y << endl << endl;
return 0;
}
void swap_by_value(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void swap_by_address(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void swap_by_reference(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
Output:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Call by value
Before swapping...
x=100, y=200
After swapping...
x=100, y=200
Call by address
Before swapping...
x=100, y=200
After swapping...
x=200, y=100
Call by reference
Before swapping...
x=100, y=200
After swapping...
x=200, y=100
Chapter.11 - this Pointer
Chapter.12 - new and delete
12.1 - new, delete and variable
1 | |
Output:
1
*p=200
1 | |
You CANNOT do this:1
2
3
4
5
6
7
8
9
10
11
12
13#include <iostream>
using namespace std;
int main()
{
int k = 100;
int *p = &k;
cout << "*p=" << *p << endl;
delete p;
return 0;
}
Output:1
2
3
4
5
6
7
8
9main.cpp: In function ‘int main()’:
main.cpp:18:12: warning: ‘void operator delete(void*)’ called on unallocated object ‘k’ [-Wfree-nonheap-object]
18 | delete p;
| ^
main.cpp:15:9: note: declared here
15 | int k = 100;
| ^
*p=100
free(): invalid pointer
Since the variable k is in stack, rather than heap.
12.2 - new, delete and Array
Chapter.13 - copy Constructor
Chapter.14 - Virtual Function, Pointer And Reference
Chapter.15 - Linked List
Part III - Java
Part IV - C
Part V - Python