Conquer the C pointer

Source: Internet
Author: User

This article is also published on my personal blog. Welcome to visit ~ Www.seekingdream.cn

After reading K & R, the knowledge of C is pointer and array. People on the Internet also feel a little "Respectful" about pointers. Recently, I learned the book "conquer the C Pointer" from my classmates. First, I came into view of the title of "Poison Tongue programmer" on the cover. I read this book over the weekend. The biggest feeling is that it is really "poisonous ". Let's take a closer look at some of your feelings!

1. When I saw this book, I thought of traveling to the west. The author of this book is "Sun Wukong" in the Travel Notes to the west. Even if it has all kinds of martial arts, it will never be able to get rid of the Wuzhishan "Ru Lai. Who is "Ru Lai? Naturally, K & R. Is the author focusing on bloggers, or is he using K & R to improve his fame? After all, there is an old saying in China: "How good a person is depends on who his opponent is ". In this book, the author points out "error points" in K & R everywhere, and holds these points to death. But who makes K & R not rigorous at the time?

2. I personally think this book is a bit "tiger-headed ". When I read the first two chapters of the book, I looked at it with reverence. After all, "the original book has been selling for 11 years", so I have gained a lot. What impressed me the most is the second chapter in this article, "how is C using memory?", which is indeed explained in depth by the author. However, starting from Chapter 3, the author has been repeatedly repeating statements in C and has been entangled in some very detailed syntax points. From this we can see that the author is indeed a profound skill, but does it give people a feeling of selling? In the fourth chapter of this book, I mainly talked about the "common methods of arrays and Pointers", which should be good in terms of application. As for the fifth chapter, since the online evaluation of this chapter is not high, and because of its purpose is to have a deep understanding of the pointer, It is skipped directly. Chapter 6 describes some traps and common usage of pointers.

The above are some of my feelings about reading this book.

Below are the notes for reading the book:

1. First, the pointer type is available. Because of the pointer type, the pointer type variables and pointer type values are available. 2. in C language standards, there are only two methods for using the main () function: int main (int argc, char * argv []) or int main (void) despite this, it is wrong to write void main (void) in some books. Some compilers will pass, but some may encounter errors. 3. for addition and subtraction of pointers, the standard only allows the pointer to point to the elements in the array, or the next element that exceeds the length of the array. 4. Add 1 to the pointer in C. The address value increases the length of the Data Type pointed to by the current pointer. 5. When constant 0 is in the context that should be used as a pointer, it is used as a null pointer. 6. If you try to pass an array as a function parameter, it will pass a pointer to the initial element. 7. When you want to pass the array value in any case, we recommend that you organize the array as a struct member. 8. The parameters declared below all have the same meaning: int func (int * a) = int func (int a []) = int func (int a [10])

Chapter 2: 1. In today's operating environment, applications are faced with virtual address space 2'. scanf: scanf () is not an explanation of the input content in row units, instead, it interprets continuous streams (the newline character is also considered a character ). If the scanf ("% d", & tmp) statement is used, scanf continuously reads characters from the stream and transforms the parts that match the format Descriptor (% d. If the input is not an integer, an error occurs. And the error characters remain in the stream. If you want to use the following statement: while (scanf ("% d", & hoge )! =-1) {/* operation */} can be replaced by a combination of fgets () and sscanf () to avoid the problem mentioned above. (Ps: For more information about fgets, see fgets. Http://blog.csdn.net/jackin2/article/details/5573771) 3, C language through malloc () Dynamic Allocation of memory areas, life until the call free. 4. In c, just as an array can be interpreted as a pointer in an expression, "function" also means "pointer to function". Generally, this pointer points to the initial address of the function. 5. The address of the automatic variable is determined at runtime. It is an object beyond the jurisdiction of the linker. Auto variables reuse the memory area. Therefore, the address of the automatic variable is not necessarily. In C, automatic variables are usually stored in the stack. By allocating automatic variables to the stack, the memory area can be reused to save memory. 6. C parameters are stacked in the stack from the back to the back. This is done to implement the Variable Length Parameter Function. It is important that the address of the first parameter is always found no matter how many parameters need to be accumulated. 7. malloc can dynamically (during runtime) allocate memory, and can be released in any order. The Region allocated by malloc is the heap, not the stack. 8. Define a variable array: char * arr; arr = malloc (sizeof (char) * len); 9. By default, C language interprets the return values of undeclared functions as int type, if a program with good luck is migrated to a processing environment with different int and pointer lengths, it will suddenly fail to run. 10. In C, forced type conversion is not required for the return value of malloc (), but in C ++, forced type conversion is required. C ++ can assign any pointer to a variable of the void * type, but it cannot assign a value of the void * type to a common pointer variable. 11. The general implementation of malloc () is to obtain a relatively large memory from the operating system at a time, and then "retail" the memory to the application. 12. After free (), if there are other addresses using this part of memory, the memory will not be immediately released. 13. Whether it is an integer or a floating point decimal, the memory format varies with the environment. 14. In the C language programming process, if a bug occurs, please solve it with the "pointer is the address" point of view-this attitude is just right to solve the bug. 15. Interpretation of C statements (1) Interpretation of Complex C statements: http://ashin.sinaapp.com/article/54/16、c, which cannot be passed by a number of groups. Only pointers pointing to the initial elements of the array can be passed. 17. const modifies the words that follow it. 18. For function parameters, the outermost array will be interpreted as a pointer, even if the number of elements is defined, it will be ignored. 19. Remember: arrays and pointers are different things. Some common pointer and array errors are used: (1) int * p; p [3] The error at this time is: Suddenly using a pointer that does not point to the memory area. The pointer to the automatic variable is in the initial state, and the value is variable (2) char str [10];... str = "abc"; the error here is: Suddenly assigning values to the array. The array is neither a scalar nor a struct, and cannot be used temporarily (3) int p []. The error here is to use null [] to declare a local variable. The array declaration is interpreted as a pointer only in the declaration of the function. 20. In an expression, an array can be interpreted as a pointer to its initial element. The array declaration can be interpreted as a pointer declaration only when the function parameter is declared.
Chapter 21: If you want to return a value other than the function return value, pass the "pointer to T" (if you want to return a value of type T) as a parameter to the function. 22. To pass an array of type T as a parameter, you can consider passing a "pointer to T ". However, the called party does not know the number of elements in the array. Therefore, if necessary, other parameters must be passed. 23. When you need to obtain a variable-length array of type T, you can use malloc () to dynamically allocate memory areas to the "pointer to T. At this time, the programmer needs to manage the number of elements in the array. 24. The pointer can refer to the next element of the last element of the array. Chapter 6 25. If strncpy () is used, note that it may generate a string without the end of a null character.

Note: for (I = 0; I <LoopMax; I ++) {/* array [I] will appear multiple times */} for (p = & array [0]; p! = & Array [Loopmax]; p ++) {/* here * p performs various processing. * P appears multiple times */} K & r tcpl once described "generally, the program using pointers is more efficient than using arrays", but I do not know the reason, now, conquer the C pointer has this description: array [I] is equivalent to the addition operation of * (array + I). If another for loop contains, when multiple addition operations are performed on the array, the efficiency is reduced. If a pointer is used, the addition operation is executed only once at the end of the loop. This is completely an error in the K & R era, and there is almost no difference in the current compiler.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.