Introduction to the Knowledge System of C Language
After learning the C language, I always wanted to write something about a knowledge system. I always felt that I was not capable enough. Finally, I have the courage to come and talk to you about how I learned the C language. Of course, it is unrealistic to traverse all the knowledge points of the C language in these thousands of words, and I am not capable of this. I will write down what is important when I study C language below. 1. Select structure program design and Loop Structure Design 1. Use of if statement 1). Compared bool I = FALSE with "zero value", what is better to initialize below?
if( i == 0); if(i == 1) // 1 if( i == TURE); if( i == FALSE); //2 if(i); if(!i); //3
Note: The if statement relies on the value of the expression in the brackets behind it for Branch jump. If the expression is true, the code followed by the if statement is executed; otherwise, the Code is not executed. Obviously, the third group is well written. It will not cause misunderstanding or errors due to different definitions of TRUE and FALSE. Of course, there is also a comparison between the float variable and the "zero value. The pointer variable is compared with the "zero value. You can study it on your own. I will not talk about it here. 2. while loop use 1). Let's illustrate an example.
#include int main() { int a = 1; int b = 2; int c = 2; int t; while (a < b < c) { t = a; a = b; b = t; c--; } printf("%d %d", a, c); system("pause"); return 0; }
What is the result? The value of a is 1, and the value of c is 0. The while LOOP also focuses on whether the value in the brackets is true. 3. The use of the for Loop is also analyzed using an example.
#include int main() { int i = 0; int arr[10] = {0}; for (i = 0; i < 10; i++) { arr[i] = i; } for (i = 0; i < 10; i++) { printf("%d\n", arr[i]); } system("pause"); return 0; }
Of course, this program is very simple, but it is enough to explain the problem. When I = 10, it will jump out of the loop. It is easy to understand. Of course, the for loop can also be rewritten using the while loop. 2. An array is a set of ordered data that stores the same data type. Notes in the array: 1. The array is assigned with a circular statement. This is also the for loop above. You can try changing the condition I <10 to I <11 on your own. The program will crash unconditionally. Because your operation is out of bounds, you have accessed the memory that does not belong to you. 2. Operation of the array subscript. Remember that the subscript of the array starts from 0, not 1 !!! 3. arrays and comma expressions confuse you.
#include int main() { int a[3][2] = { (0, 1), (2, 3), (4, 5) }; int *p = a[0]; printf("%d", p[0]); system("pause"); return 0; }
At this time, p [0] is equal to 1. Why should it be 0? In this case, the comma expression is obtained. The last value of the comma expression is the value of the entire expression, so the code above becomes int a [3] [2] = {1, 3, 5 }; if so, will you do the right thing? 4. Storage of two-dimensional arrays in memory
int arr[3][3] = {0};
2D arrays are linearly stored in memory. We can use a one-dimensional array to understand two-dimensional arrays. The above Code represents a two-dimensional array with three elements: arr [0], arr [1], arr [2], at the same time, each element has three elements, arr [0] [0], arr [0] [1], arr [0] [2]..., in this way, arr [0], arr [1], and arr [2] are the names of One-dimensional arrays, however, these three one-dimensional arrays are associated and are continuously opened in the memory. Similarly, we can push it to a three-dimensional array until the n-dimensional array. As long as you have time and perseverance and determination (little significance ). Haha !! 5. How to assign values to character arrays? Why do we need to assign values to character arrays? I think this is closely related to string operations. Let's look at two different assignment methods. 1 ).
char arr[]={'i',' ','a','m',' ','h','a','c','k','e','r'};
It is foreseeable that the string stored in this character array does not use a series of string operation functions. If you do not know much about common string operation functions, visit the following link, http://10799170.blog.51cto.com/10789170/1715083 helps you understand the implementation of common string operation functions and precautions. The second method is to assign values. 2). char arr [] = "I am hacker"; this assignment method is the most common and tested. We recommend that you use the assignment of character arrays in this way. "\ 0" is not lost in this assignment method ". So the question is, can I assign a value like this?
char arr[] = {"i am hacker"}; char arr[] { = "i am hacker"};
The answer is yes. Interested readers can see it in chapter 2, section 6th, "deep analysis of C language. 6. Summary: 1). When assigning values to the array in the memory, pay attention to the out-of-memory issue. 2) subscript operation of the array. 3). "\ 0" of the character array ". 4) assign values to two-dimensional arrays. 3. the pointer is the address. When I was a beginner, I couldn't tell clearly what the pointer and the pointer variable differ from the content in the address it points to. Fortunately, I learned about the pointer for one afternoon. Thanks to a book called "c and pointer", it is clear and easy to understand. 1. A level-1 pointer defines a level-1 pointer 1 int * p = NULL. First, the pointer is the address, and the pointer variable is the address used to save the space pointed to by the pointer, "*" is the key to open the memory. Second, pointer definition and assignment are two different things. Finally, I have written my own understanding of pointers and array pointers. If you are interested, open the following link. 1) pointer joke (implemented in vc6.0)
Int * p = (int *) ox12ff7c; // This address must be the first address in the memory that begins to allocate the address to the variable. If not, // will not work! * P = NULL;
You can try the result on your own. 2) Talk about the pointer Security pointer can point to any address in the memory, but in a program, there is always only its own stack frame, or the memory stored in static de-and stack. If you accidentally access your memory and change the value. Debugging is a huge task for a large program. Therefore, be careful when using pointers. Put an end to the wild pointer !! 3). The const and assert modifiers do not need to modify the value pointed to by the pointer. Please add the const modifier.
const int *p; int const *p; int * const p; const int * const p;
Readers can try to analyze what the preceding statements represent. The assert only takes effect in the debug version and does not make sense in the release version. The debug version can help us verify the validity of the pointer. Large companies pay great attention to this issue during interviews. The books I read when I was learning are "deep analysis of c language" and "c and pointer". Readers can learn more by themselves. 4. When talking about memory operations, I have to mention stack frames. Readers can refer to the url attached to a blog post I wrote. http://10799170.blog.51cto.com/10789170/1715186 Here we will mainly discuss the issues that should be paid attention to when malloc is used to open up memory. 1) After dynamically opening up the memory, you need to determine whether the memory is successfully opened. If the memory is not opened up successfully, direct use will lead to access out of bounds; 2). After use, you must use the free function to release the memory. The principle is to disconnect the pointer from the memory. Memory leakage may occur. Dangerous! 3) after use, the pointer should be assigned as NULL to prevent the wild pointer. If the pointer is not assigned as NULL in time, the pointer will point to the "junk" memory! 5. This part of function functions is unwilling to come up with them. After all, almost everyone who learns C language is writing functions. So let's summarize the benefits of the function. 1). reduce complexity 2). Avoid repeating code 3). Restrict the impact of changes. 4). Implicit sequence 5). Improve performance 6). For more information about centralized control, see in-depth analysis of C language. 6. Let me talk about the structure. I will not talk about the syntax. 1. struct is a custom type. You can use this type to define struct variables. 2. struct is different from arrays. It can combine different data types in a set. 3. It is the node that implements the linked list. 4. I have implemented a simple phone book using a struct and attached a url