C language programming experience gradually accumulated 3

Source: Internet
Author: User
Tags switch case

I wrote a few notes about the blind spots in my C language. I only thought that I had experienced and had errors.

1. typedef is used to define a function pointer type method and a new function pointer type. : It is easy to create a type alias. In the traditional variable declaration expression, replace the variable name with the type name, and add the keyword typedef to the beginning of the statement ". Typedef int (* PFUN) (): defines the PFUN function pointer type, which is automatically completed by the compiler. For example, PFUN pfun; defines a function pointer, it is similar to abstracting a new variable type.
Typedef unsigned short (* pTaskEventHandlerFn) (unsigned char task_id, unsigned short event );
PTaskEventHandlerFn tasksArr []; // declare an array of function pointers
Use typedef to replace a type name with the variable name originally stated, and then the type name can be used to declare and define the variable.
2. An array definition with initialization can omit the array size in square brackets.

For example, the following code defines an array as five elements:
Int a [] = {2, 4, 6, 8, 10 };
  The size of the array must be known during compilation.Usually, the number in square brackets determines the size of the array. If an array definition is initialized and the array size in square brackets is omitted, the compiler calculates the number of elements between brackets to obtain the array size.
For example, the following code produces the same result:
Static int a1 [5] = {1, 2, 3, 4, 5 };
Static int a2 [] = {1, 2, 3, 4, 5 };

2. The top of the stack is generally a low address, and the function entry stack is usually carried out from right to left.
3. for (a; B; c) {e}: first perform a, then B, execute e, and then execute c. further execute B, execute e, c. until B does not meet the exit cycle.
4. union data type, memory overlapping, exactly the same as typedef union {
Uint32 time32;
Uint16 time16 [2];
Uint8 time8 [4];
} OsalTime_t;
After a value is assigned to time32, the original memory is used for accessing other types. The three members occupy the same data space and the starting address is the same. Once the value of a Member changes, the values of other members also change.
5. inline function summary inline functions have a small amount of Code and are directly compiled into the executable file without calling, that is, if functions in different files call inline functions, then different functions will show the executable code of this inline function. However, if you do not call the function repeatedly, the advantage of the inline function is that the execution is faster, no call is required, and the function parameter's inbound/outbound SP is used to protect the stack frame and jump out of the function, stack frame clearing and other time consumption. To achieve quick execution. Therefore, the amount of code for inline functions is small. In short, it increases the execution speed by increasing the execution file size.
6. (int *) 0, which indicates pointing to an integer address. The address value is 0, that is, the place where the NULL pointer is located. Write operations are generally not allowed here to avoid damage to the system, but can be read.
7. if a function is to be used by functions in other files, the extern keyword is added during definition. If the extern and static keywords are not added, the compiler usually uses the extern type by default, therefore, you can also use it in other functions.
8. detailed description of the structure's offsetof macro, # define offsetof (TYPE, MEMBER) (size_t) & (TYPE *) 0)-> MEMBER) (include/linux/stddef. h)
The 9.32-bit machine reads one byte and further processes the actual underlying assembly. For example, if you want to read a byte, the actual return value is a 32-bit value, and the number of the addresses you want is obtained by shielding the shift. The same is true when writing data. The other four bytes are retained, and the bytes to be written are changed separately. The compiler implements the actual implementation based on your data type. 32-byte reading is the most efficient.
10. When a service interruption occurs and the service process is not completed, the Pending will be ignored. A high-priority interruption can obtain the right to use the interruption. Another similar interruption occurs until the processing of the Current Interruption is completed.
11. Incorrect switch case. Switch case often performs classification operations on different commands, but if the corresponding case is processed, if the break is not met, the following case content will be executed by default (the case will not be removed, this is because the case will be matched only when the switch is started. When the switch is started, the case matching will be ignored ). The case is skipped until it encounters a break. Otherwise, the execution is complete.
This is because the switch is made only for the first time and jumps to the corresponding content for execution. Therefore, a good program basically requires break, but in some cases (the content to be processed in multiple cases should be the same, that is, the previous several types can not be processed, without break, until the last one is processed, the same function is executed for multiple types, but the amount of code is not increased, more direct ). In short, you only make judgment and selection for the first time. After Entering, the installation sequence is executed. If you do not run break, the switch will be executed until the end of the installation.
12. Exclamation point in C language! Logic inversion, different from ~ Bitwise inversion! =: Not equal .! (1) 0 ,! (0) indicates 1, indicating that it has the opposite intention, but it can only be used on 0 and 1. If the value is not equal to 0, it will change to 0. If the value is equal to 0, it will change to 1.

13. C language ++ and < <遇到的一个bug问题。在这里+的优先级高于<<左右移。所以比如a<<8+b,实际是从右往左赋值,8+b相加后来移动a,所以不实现两个字节合并为一个16位的数据。故要加(a<<8)+b才能实现本来的意图,这个误区需要注意。
14.: # define PINMUX0_31_28 0x00000001u, 0x00000001ULu indicates unsigned, UL unsigned long int 32-bit, unsigned long 64-bit
15 differences between array pointers and pointer arrays an array pointer is a pointer, int (* a) [10]; pointing to an array, similar to a function pointer array, which is an array, int * a [10]; returns a pointer similar to a pointer function.
16. The absolute path file under the window needs to be represented by a double slash \\
17 printf ("c = % # x \ n", c); what does it mean?
% # Indicates the output prompt mode. If it is in octal format, add 0 to the front. If it is in decimal format, no characters are added. If it is in hexadecimal format, 0x is automatically added.

18 static variables are initialized during compilation, so the initial value must be a constant (can be a constant, a macro defined as a constant, or a const modified in the C ++ compiler ), static variables cannot be assigned an initial value, but can be assigned a value at runtime.
19. The assert function is used to determine whether it is true. If it is true, it will continue to be executed. Otherwise, the program will be terminated directly. 20. 4-byte alignment. Use the following method: (CSL_CacheRegsOvly) CSL_CACHE_REGS)-> L1DIWC = (byteCnt + 3)> 2); when the number of bytes is not a multiple of 4, the number of characters to be retained, so we can add 3, 1 + 3 + 3 + 3.4 bytes to get the byte number after 4 bytes alignment: byte_num = (byte + 3) & 0xfc; the number of bytes must be a multiple of 4.
21. If the break is in the for loop, I ++ will not execute it.
22. temp = (temp + 1) & 7. It indicates that temp changes from 0 to 7 and loops. Similar to temp = 8, the value is 0.
23. unsigned char * p; unsigned char a []; p = a; this is correct.
Unsigned int p1 = a; error, because the pointer type is assigned to the integer data type, the type does not match. Unsigned char * p [10]: declares a pointer array with 10 pointers. Each Pointer Points to a char-type space. Unsigned char (* p) [10] declares an array pointer pointing to a space containing 10 char Types. the pointer maintains a space of 10 and offsets 1 and 10 spaces.
24. Multiple if statements are processed in different branches, and then whether or not to execute them. When the if clause works with multiple else if statements, the following else if statement is not executed when a condition is met, so that only one branch is entered.

25. char a [3] [4]. char ** p = a; a compilation error occurs. If the type does not match, the value cannot be assigned because a represents a pointer to a one-dimensional array, that is, char (* a) [3. a + 1 is offset by three units. P is a pointer, called a second-level pointer. So the two types are different.
26. in "C and pointer" p142, array names are not represented by pointer constants in the following two scenarios, when the array name is used as the operand of the sizeof operator and the single object operator. Sizeof returns the length of the entire array, instead of the length of the pointer to the array. Getting the address of an array name produces a pointer to an array instead of a pointer to a pointer constant.
Therefore, the pointer returned after & a is the pointer to the array (similar to (* p) [3]), and a (a pointer to the [0] element) there are differences in Pointer types. & A + 1 is the size offset from an array sizeof, because & a is the address pointing to the array pointer and a value pointer. As a parameter, the first-level array name is converted to a common first-level pointer.
27. The two-dimensional array name cannot be simply understood as a 2-level pointer, but should be understood from the system structure of the array. I personally think that the two-dimensional array name is actually a pointer, But it points to the Data Type of the one-dimensional array. That is, the value pointer can be understood as a function similar to a two-dimensional array name. For data access, the compiler automatically accesses the stack zone.

28. The value name cannot be modified because ebp and base address have been set in the stack area. The query array is based on this ebp. Therefore, assigning values to the array name makes no sense and it is certainly not allowed to modify the ebp. Therefore, an error is reported during compilation.
29. fseek and tell work together to determine the file size. Fseek (fPtr, 0, SEEK_END); file_size = ftell (fPtr); fseek (fPtr, 0, SEEK_SET), position pointer reset for read/write operations

30. two-digit exchange does not use the third buffer. The three methods of code are mainly implemented through operators.
A = A ^ B; B = A ^ B; A = B ^;
A = A + B; B = A-B; A = A-B;





Related Article

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.