"1. To operate a wave, let's first experience whether a wave #include" XXX. C "file can be used:
Reference Demo:
1 / / filename: main 2#include stdio. H 3#include stdlib. H 4 5 / ******************************************************************************************* 6 *. C file declaration area 7 ************************************************************************************* c, char *argv[]) { 16 17 Fuction1(); 18 Fuction2(); 19 printf ("welcome to the official account: last bug"); 20 return 0; 21}
1 / / filename: module1. C 2#include stdio. H 3 / ********************************* 4 * fusion: fuction1 5 * Author: (last Bug) 6 ******************** / 7void fuction1 () 8{9 printf ("run fuction1"); 10}
1 / / filename: module2. C 2#include stdio. H 3 / *********************************** 4 * fusion: fuction2 5 * Author: (last Bug) 6 **************************** / 7void fuction2 () 8{9 printf ("run fuction2"); 10}
Output results:
Analyze:
It seems that this wave of operation is feasible, and the. H file seems to be omitted. As the bug bug said before, when analyzing the. H file, directly expand the. H file at the position in the corresponding. C file, and then further analyze it. In fact, this. C file is the same, and then look down.
Reference Demo:
1//FileName :main 2#include 《stdio.h》 3#include 《stdlib.h》 4 5char * cBug1 = “bugNo1”; // Here is position 1 6char * cbug2 = "bugno2"; 7 / ************************************************** / 10#include "module1. C" 11#include "module2. C" 12 13 / / char * cbug1 = "bugno1"// Here is position 2 14 / / char * cbug2 = "bugno2"; 15 16 / *************************** 17 * fusion: Main 18 * Author: (last Bug) 19 ************************* / 20int main (int argc, char * argv []) {21 22 fusion1(); 23 Fuction2(); 24 printf ("welcome to the official account: last bug"); 25 return 0; 26}
1 / / filename: module2. C 2#include stdio. H 3 / ********************************** 4 * fusion: fuction1 5 * Author: (last Bug) 6 ********************** / 7void fuction1 () 8{9 printf ("run fuction1"); 10 printf(“%s ”,cBug1); 11}
1 / / filename: module2. C 2#include stdio. H 3 / *********************************** 4 * fusion: fuction2 5 * Author: (last Bug) 6 **************************** / 7void fuction2 () 8{9 printf ("run fuction2"); 10 printf(“%s ”,cBug2); 11}
Output results:
Analyze:
We define two variables in position 1, and the above results are obtained after successful compilation and operation, which is in line with our expectations. However, when we remove the definition of position 1 and position 2, the program cannot be compiled. It seems that it is consistent with our expectation to directly expand the. C file in the compilation process.
2. What's the use? In the long coding history of bug bacteria, this method is generally used only in two cases:
1. Maintain code without design
Some projects with a long history have been ravaged by more than n leaders. In fact, the code structure has been very terrible. Often, the content of each source file is very long. In order to keep the code as it is, we will use #include "XXX. C" to embed these related files for later maintenance.
2. Test code
In the early stage of software debugging, you may insert different test function functions in different files. In this way, you can easily introduce and eliminate them.
For example, you need to monitor some static variables in the source file, but you don't want to add test code in this file, so you can write test functions in #include "XXX. C" for use, such as:
1//FileName :main 2#include 《stdio.h》 3#include 《stdlib.h》 4 5static int a = 5; 6 / ************************************************ 7 *. C file declaration area 8 *********************** / 9#include "module1. C" 10 11 / *************************************************************************; 18 printf(“main %d ”,a); 19 printf ("welcome to the official account: last bug"); 20 return 0; 21}
1 / / filename: module2. C 2#include stdio. H 3 / ********************************** 4 * fusion: fuction1 5 * Author: (last Bug) 6 ********************** / 7void fuction1 () 8{9 printf ("run fuction1"); 10 printf(“Fuction1 %d ”,a); 11}
Attention!!
Then, a little friend said, "the scope of static is only in the corresponding file". Using static a variables through multiple. C files above, the little partner's expression is not so appropriate! 3. Technical summary: in the normal development process, we still don't recommend using #include "XXX. C" because in the design process of our program, the. H file is an external reference interface, and. C is the corresponding internal implementation. If you abuse #include "XXX. C", it may cause repeated definitions of functions, etc., and also bring some troubles to debugging related programs, Of course, if you can do it well, there will be no problem. However, it's a blessing for small partners who like to write long files. Divide a long. C file into multiple. C files, so that they can at least show their colleagues who don't know this usage!
two
void
1. Let's briefly understand that void void in most small partner programs is only used for function without parameter input or type return. However, the variables we usually define have specific types, such as int, float, char, etc. is there a variable of void type? You can experiment. The answer is: No, compilation will make mistakes.
It is obvious in the figure above that the compiler is not allowed to define variables of void type. Variables need to occupy a certain amount of memory. Since void indicates no type, the compiler naturally does not know how much memory to allocate, resulting in compilation failure. Although void cannot directly modify variables, it can be used to modify the pointing of pointers, that is, the untyped pointer void *, which makes sense. The untyped pointer does not have to point to untyped data, but can point to any type of data. 2. Basic operation of void * in fact, you have encountered the use of void * when using dynamic memory allocation. Let's take a look at the prototype definitions of the following standard functions:
The above functions are related to memory operation. Some small partners may have used them and may not know the specific type of each parameter. The arguments passed in by the formal parameters of the void * part do not need forced type conversion, so there is no need to pay attention to the specific type pointed to by the incoming pointer, However, the void * returned by the function generally needs to be converted to the corresponding specific type through forced type, unless the last variable you pass is also void * type.
Reference void * usage:
1 #include stdio. H 2 #include stdlib. H 3 #include malloc. H 4 5#define num 10 6 / ********************************************** 7 * fusion: learn about the use of void * 8 * Author: (last Bug) 9 *************************************************************************, char *argv[]) { 11 int *p1 = (int *)malloc(NUM*sizeof(int)); 12 int *p2 = (int *)malloc(NUM*sizeof(int)); 13 int i = 0; 14 15 / / initialize P1 16 for (I = 0; i 《 NUM; i++) 17 { 18 *(p1+i) = i; 19} 20 / / perform memory copy 21 memcpy (P2, P1, Num * sizeof (int)); 22 23 / / output another allocated memory 24 for (I = 0; i 《 NUM; i++) 25 { 26 printf(“%d,”,*(p2+i)); 27} 28 / / free memory 29 free (P1); 30 free(p2); 31 return 0; 32}
Operation results:
3. Using void * to realize typeless data encapsulation. In order to maintain the integrity of the article, perhaps this is what the author most wants to introduce to you. Since void * is so flexible, it must be very useful. If it is only used to simply transfer parameters, it seems a little overqualified, so we have to use it in the upper software design. In some projects, it is often seen that small partners turn data types around, and sometimes even have to rewrite a function with only different data types for the change of a data type. Such code is tens of thousands of lines of code, According to the following example, we will introduce a method to reduce the increase of program duplicate code caused by data type changes.
Reference examples:
1 #include stdio. H 2 #include stdlib. H 3 / ************************************ 4 * fusion: add 5 * descir: relevant data and processing methods of addition 6 * Author: (the last Bug) 7 ***************************************************** / 8typedef struct_ tag_ Add 9{ 10 int a; 11 int b; 12 int result; 13}sAd
Our other product: