[Book] Mastering C/C++ Pointer

First Post:

Last Update:

Word Count:
2.4k

Read Time:
15 min

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

int main()
{
int x = 100, y = 200, z = 300;
printf("Address of x: %p\n", &x);
printf("Address of y: %p\n", &y);
printf("Address of z: %p\n", &z);

printf("Value of x: %d\n", x);
printf("Value of y: %d\n", y);
printf("Value of z: %d\n", z);

return 0;
}

Output:

1
2
3
4
5
6
Address 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
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
#include <stdio.h>

int main()
{
int x[5];
int i;
for (i = 0; i < 5; i++) {
printf("Enter x[%d]: ", i);
scanf("%d", &x[i]);
}

for (i = 0; i < 5; i++) {
printf("x[%d] = %d\n", i, x[i]);
}
printf("\n");

for (i = 0; i < 5; i++) {
printf("Enter x[%d]: ", i);
scanf("%d", x + i);
}

for (i = 0; i < 5; i++) {
printf("x[%d] = %d\n", i, x + i);
}
for (i = 0; i < 5; i++) {
printf("x[%d] = %d\n", i, x[i]);
}

return 0;
}

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
Enter 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int main()
{
int x[5] = {10, 20, 30, 40, 50};
int i;

for (i = 0; i < 5; i++)
printf("&x[%d] = %p\n", i, &x[i]);
printf("\n");

for (i = 0; i < 5; i++)
printf("x+%d = %p\n", i, x+i);
printf("\n");

for (i = 0; i < 5; i++)
printf("x[%d] = %d\n", i, x[i]);

getchar();

return 0;
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{
int x = 100, y = 200;
int *ptr = &x;
printf("Address of x: %p, value: %d\n", &x, x);
printf("Address of y: %p, value: %d\n", &y, y);
printf("Address of ptr: %p\n\n", &ptr);

printf("Content of ptr: %p\n", ptr);
printf("x = %d, *ptr = %d\n\n", x, *ptr);

ptr = &y;
printf("Content of ptr: %p\n", ptr);
printf("y = %d, *ptr = %d\n", y, *ptr);

getchar();

return 0;
}

Output:

1
2
3
4
5
6
7
8
9
Address 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main()
{
int x = 100;
int *p1 = &x;
int **p2 = &p1;

//int p3 = &x; Compile error.
//int *p4 = &p1; Compile error.

printf("&x = %p\n", &x);
printf("&p1 = %p, p1 = %p\n", &p1, p1);
printf("&p2 = %p, p2 = %p\n", &p2, p2);

printf("**p2 = %d, *p1 = %d\n", **p2, *p1);

return 0;
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main()
{
int x = 100;
int *p1 = &x;
int **p2 = &p1;
int ***p3 = &p2;

printf("&x = %p\n", &x);
printf("&p1 = %p, p1 = %p\n", &p1, p1);
printf("&p2 = %p, p2 = %p\n", &p2, p2);
printf("&p3 = %p, p3 = %p\n\n", &p3, p3);

printf("***p3 = %d, **p2 = %d, *p1 = %d\n", ***p3, **p2, *p1);

return 0;
}

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
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
#include <stdio.h>

int main()
{
int a = 100, b = 200;

const int *p = &a;
int *const q = &b;
printf("a = %d, *p = %d\n", a, *p);
printf("b = %d, *q = %d\n", b, *q);

/* NO!
*p = b;
*/

/* OK */
p = &b;
printf("*p = %d\n", *p);

/*-------------*/

/*NO!
q = &a;
*/

/* OK */
*q = 888;

return 0;
}

Output:

1
2
3
a = 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
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;
}

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
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
#include <stdio.h>

int add(int, int);
int substract(int, int);
int multiply(int, int);
int divide(int, int);
int (*operation)(int, int);

int main()
{
int x, y, output;
printf("Enter x and y: ");
scanf("%d %d", &x, &y);
operation = add;
output = (*operation)(x, y);
printf("%d + %d = %d\n", x, y, output);

operation = substract;
output = (*operation)(x, y);
printf("%d - %d = %d\n", x, y, output);

operation = multiply;
output = (*operation)(x, y);
printf("%d * %d = %d\n", x, y, output);

operation = divide;
output = (*operation)(x, y);
printf("%d / %d = %d\n", x, y, output);

return 0;
}

int add(int a, int b)
{
return a + b;
}

int substract(int a, int b)
{
return a - b;
}

int multiply(int a, int b)
{
return a * b;
}

int divide(int a, int b)
{
return a / b;
}

Output:

1
2
3
4
5
Enter 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
17
Call 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
2
3
4
5
6
7
8
9
10
11
12
13
using namespace std;

int main()
{
int *p = (int *)malloc(sizeof(int));
//int *p = new int;
*p = 200;
cout << "*p=" << *p << endl;

delete p;

return 0;
}

Output:

1
*p=200

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
9
main.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