Section 9: Pointers

Introduction

1. Two types of variables.
• data variable. For example, int x = 10;.
• address variable. For example, int *p; p = &x;
2. Example
• declaration int *p;.
• initialization p = &x;.
• dereferencing cout << *p;.

Why pointers?

1. 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.
2. 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

1. 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.
2. 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 use delete []p;. If you really want to make p point to nothing, you should use p = nullptr;.
3. You can access general array by using A[2], you can also access pointer array by using p[2]. It’s the benefit.

Pointer Arithmetic

1. p++;, ++ means move to next location instead of add one. For example, if the pointer is float, it will move 4 bytes.
2. p--;, – means move backward.
3. p = p + 2;, it will move forward by two elements.
4. p = p - 2;, it can move backward.
5. 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 do p - q, it will get the -3. You can know q is farther than p.
6. Print values by many ways.
7. Print distance example.

Problems using Pointers

1. Uninitialized pointer.
• There are three ways to initialize the pointer.
2. 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.
3. Dangling pointer

Reference

1. Reference doesn’t consume any memory at all.
2. Reference Example.

Section 10: String

Introduction

1. There are two ways to represent the String.
• Using char Array
• class string
2. 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";
3. If you put the letters after \0, those letters won’t show.

1. You can use cin.get() and cin.getline() to write a string.
2. If you try to get two string using cin.getline(), remember to use cin.ignore() to avoid the second cin get extra content from the first cin.

String Functions - Length, Concatenate and Copy

1. strlen(str1); to get the length of string.
2. strcat(destination, source); concatenates two words.
3. strncat(destination, source, length); concatenates two words with certain length.
4. strcpy(destination, source); copies source string to destination string.
5. strncpy(destination, source, length); copies source string to destination string with certain length.

String Function - Substring and Compare

1. strstr(main, sub); can substring with certain string, it returns the string from sub to the rest of main string.
2. If you use strstr in the sub string which is not in the main string, it returns null.
3. The function of strchr(main, char); is as same as strstr, the only difference is the second parameter is char type.
4. strrchr(main, char); can return the string count from the right hand side.
5. strcmp(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.

String Functions - Tokenizer and To Integer

1. strtol(str1, NULL, base) means string to long. strtof(str1, NULL) means string to float.
2. strtok(str1, "=;") tokenize a string base on the symbols.

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.

Share