In the past two days, we used the structure to simulate the object-oriented method and wrote the hardware driver in the C language.Program. As follows:
A structure is defined in ds1302.h:
Typedef struct {
Void (* fun1 );
Void (* fun2 );
} Ds1302_t;
Extern ds1302_t ds1302;
A global variable is declared to be called in the main. c file where the main function is located, and the variable is defined in ds1032.c:
Ds1302_t ds1302;
Then initialize in ds1032.c:
Ds1302 = {fun1, fun2}; // among them, fun1 and fun2 is the previously defined function as a structure member. However, before initialization, the error message is "repeated definition. Think twice about it.
Finally, a simple program (test. c) is created with VC)
# Include <stdio. h>
Typedef struct {
Unsigned char;
} Student;
Student S1;
S1.a = 0;
Void main ()
{
}
The above files cannot be compiled, and the prompt is the same. If you repeat the definition, you will find that you have previously assigned a value to the variable before the main function. The main function is used to declare and define global variables and functions, and is pre-processed by the compiler to allocate memory and addresses to those global variables and defined functions. It cannot be designed to be executable.Code.
In the compilation phase, the compiler allocates memory for the global variables before the main function, generates executable code for the defined function, and retains the address of the function for the main function to call, the value assignment statement operation is an executable code, and there is no such thing as a function pointer for the main function to call, so it can only be executed in sequence within the main function.
In multi-file organization, only the main function in the source file containing the main function can assign values to variables, other source files that do not contain main cannot assign values to variables. They can only declare variables.
But it does not mean that you cannot initialize and assign values to variables before the main function: You can initialize and assign values to variables when defining variables. As follows:
Int A = 10;
Student S1 = {10 };
These are all possible.