
Section 9: Pointers
Introduction
- Two types of variables.
- data variable. For example,
int x = 10;
. - address variable. For example,
int *p; p = &x;
- data variable. For example,
- Example
1
2
3
4
5
6
7// x, value 10, address 200/201
// p, value 10, address 300/301
cout << x; // 10
cout << &x; // 200
cout << p; // 200
cout &p; // 300
cout << *p. // 10 - Three things about pointer.
- declaration
int *p;
. - initialization
p = &x;
. - dereferencing
cout << *p;
.
- declaration
Why pointers?
- You can only access a file by pointer. Also, access network connection can only be done by pointer. The keyboard, monitor and printer are all needed it.
- Java and most of programming languages without pointer can’t access the hardware directly. For example, Java need to use JVM to access hardwares using C runtime.
Heap Memory Allocation
- When you initiate
int *p
, p will created in the stack of memory. Then, if you new array, the array will created in the heap of memory and p will point to the array in the heap.1
2
3int A[5] = {1, 2, 3, 4, 5};
int *p;
p = new int[5]; - When you want to delete a pointer variable p, you should not use
p = NULL;
. The p won’t be deleted, p just point to null. You should usedelete []p;
. If you really want to make p point to nothing, you should usep = nullptr;
. - You can access general array by using
A[2]
, you can also access pointer array by usingp[2]
. It’s the benefit.
Pointer Arithmetic
p++;
, ++ means move to next location instead of add one. For example, if the pointer is float, it will move 4 bytes.p--;
, – means move backward.p = p + 2;
, it will move forward by two elements.p = p - 2;
, it can move backward.d = q - p
, it can know the distance between two elements. For example,p = 200
,q = 206
, and the type of p and q is int. The d will be 3, because it will divide 6 by 2 bytes and you know the distance between the q and p is 3 elements. On the contrary, if you dop - q
, it will get the -3. You can know q is farther than p.- Print values by many ways.
1
2
3
4
5
6
7
8
9
10
11
12
13
14int A[5]{2, 4, 6, 8, 10};
int *p = A;
for (int i = 0; i < 5; i++) {
// cout << A[i] << endl;
// cout << i[A] << endl;
// cout << *(A + i) << endl;
// cout << A + i << endl; // print the address
// cout << p + i << endl; // print the address
// cout << *(p + i) << endl;
// cout << p[i] << endl;
cout << *p << endl;
p++;
} - Print distance example.
1
2
3
4
5
6int A[5]{2, 4, 6, 8, 10};
int *p = A, *q = &A[4];
cout << p - q;
====OUTPUT====
-4
Problems using Pointers
- Uninitialized pointer.
- There are three ways to initialize the pointer.
1
2
3
4
5
6
7int x = 10;
int *p;
1) p = &x;
2) p = (int *) 0x5638;
3) p = new int[5];
// Runtime error - Using uninitialized pointer
cout << *p; - Memory leak.
- If you don’t want to use p anymore, and you don’t delete the p by using
delete []p
. It will cause memory leak.
1
2
3
4
5int *p = new int[5];
.
.
.
p = NULL; - If you don’t want to use p anymore, and you don’t delete the p by using
- Dangling pointer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16void main() {
int *p = new int[5];
.
.
.
fun(P);
// cause the error, since p is deleted in the function.
cout << *p;
}
void func(int *q) {
.
.
.
delete []q;
}
Reference
- Reference doesn’t consume any memory at all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17main() {
int x = 10;
int &y = x; // reference, x is l-value of x
// Error: cannot initialize without value.
// int &y;
x++;
y++;
cout << x; // 12
cout << y; // 12
int a;
a = x; // x is r-value and the data of x.
x = 25; // x is l-value and the address of x.
// Error: y can't be referencing any other variable at all.
// &y = a;
} - Reference Example.
1
2
3
4
5
6
7
8
9
10
11
12int x = 10;
int &y = x;
cout << x << endl;
y++;
x++;
cout << x << endl;
cout << &x << " " << &y << endl;
====OUTPUT====
10
12
0x7ffee33ae828 0x7ffee33ae828
Section 10: String
Introduction
- There are two ways to represent the String.
- Using char Array
- class string
- Declaring and Initializing String
char x = 'A';
char S[10] = "Hello";
char S[] = {'H', 'e', 'l', 'l', 'o', '\0'};
char S[] = {65, 66, 67, 68, '\n'};
char * S = "Hello";
- If you put the letters after
\0
, those letters won’t show.1
2
3
4
5
6
7char H[] = {'H', 'e', 'l', 'l', 'o', '\0', 'p', 'p', 'p'};
cout << H << endl;
char S[] = {65, 66, 67, 68, 69, 0, 70, 71};
cout << S << endl;
====OUTPUT====
Hello
ABCDE
Reading and Writing String
- You can use
cin.get()
andcin.getline()
to write a string. - If you try to get two string using
cin.getline()
, remember to usecin.ignore()
to avoid the second cin get extra content from the first cin.1
2
3
4
5
6
7
8
9
10
11
12
13
14char s[100];
char s2[100];
cout << "Enter your Name ";
cin.get(s, 100);
cout << "Welcome " << s << endl;
cin.ignore();
cout << "Enter your Name Again ";
cin.get(s2, 100);
cout << "Welcome " << s2 << endl;
String Functions - Length, Concatenate and Copy
strlen(str1);
to get the length of string.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char *s;
cout << "Enter a String ";
cin.getline(s, 100);
cout << "Length " << strlen(s) << endl;
return 0;
}
====OUTPUT====
Enter a String Hello World
Length 11strcat(destination, source);
concatenates two words.1
2
3
4
5
6
7
8char S1[20] = "Good";
char S2[10] = "Mourning";
strcat(S1, S2);
cout << S1 << endl;
====OUTPUT====
GoodMourningstrncat(destination, source, length);
concatenates two words with certain length.1
2
3
4
5
6
7
8char S1[20] = "Good";
char S2[10] = "Mourning";
strncat(S1, S2, 3);
cout << S1 << endl;
====OUTPUT====
GoodMoustrcpy(destination, source);
copies source string to destination string.1
2
3
4
5
6
7
8char S1[20] = "Good";
char S2[10] = "";
strcpy(S2, S1);
cout << S2 << endl;
====OUTPUT====
Goodstrncpy(destination, source, length);
copies source string to destination string with certain length.1
2
3
4
5
6
7
8char S1[20] = "Good";
char S2[10] = "";
strncpy(S2, S1, 2);
cout << S2 << endl;
====OUTPUT====
Go
String Function - Substring and Compare
strstr(main, sub);
can substring with certain string, it returns the string from sub to the rest of main string.1
2
3
4
5
6char S1[20] = "Programming";
char S2[10] = "gram";
cout << strstr(S1, S2) << endl;
====OUTPUT====
gramming- If you use
strstr
in the sub string which is not in the main string, it returns null.1
2
3
4
5
6
7
8
9char S1[20] = "Programming";
char S2[10] = "k";
if (strstr(S1, S2) != NULL)
cout << strstr(S1, S2) << endl;
else
cout << "Not Found" << endl;
====OUTPUT====
Not Found - The function of
strchr(main, char);
is as same asstrstr
, the only difference is the second parameter is char type. strrchr(main, char);
can return the string count from the right hand side.1
2
3
4
5
6
7char S1[20] = "Programming";
cout << strchr(S1, 'g') << endl;
cout << strrchr(S1, 'g') << endl;
====OUTPUT====
gramming
gstrcmp(str1, str2)
will return 0 when the two strings are the same. It will return positive number if str1 is greater than str2, and vice versa.1
2
3
4
5
6char s1[20] = "minor";
char s2[20] = "elder";
cout << strcmp(s1, s2) << endl;
====OUTPUT====
8
String Functions - Tokenizer and To Integer
strtol(str1, NULL, base)
means string to long.strtof(str1, NULL)
means string to float.1
2
3
4
5
6
7
8
9
10char s1[10] = "235";
char s2[10] = "54.78";
long int x = strtol(s1, NULL, 10);
float y= strtof(s2, NULL);
cout << x + 10 << endl << y - 5 << endl;
====OUTPUT====
245
49.78strtok(str1, "=;")
tokenize a string base on the symbols.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16char s1[20] = "x=10;y=20;z=35";
char *token = strtok(s1, "=;");
while (token != NULL)
{
cout << token << endl;
token = strtok(NULL, "=;");
}
====OUTPUT====
x
10
y
20
z
35
Disclaimer
I took this course from Udemy, which is Learn C++ Programming -Beginner to Advance- Deep Dive in C++. I only took some notes of this amazing course for my personal future uses and share my thoughts with my peers. If you like it, you should take the course from Udemy too.