The C ++ function must have the main () function. The main () function is the entry point of the program. Each sample program described earlier has the main () function. However, not all C ++ programs have traditional main () functions. Windows program entry point functions written in C or C ++ are called WinMain () instead of traditional main () functions.
It indicates that the C ++ Builder GUI application has WinMain (), but is hidden. C ++ functions enable users to focus on the creation of user interfaces and other parts without considering the low-level details of Windows programs. The main () function is similar to other functions and has the same components.
In a 32-bit console application, C ++ Builder generates the default main () function with the following prototype: int main (int argc, char ** argv); this main () take two parameters in the function form and return an integer value. As mentioned above, numeric values are passed to the function when a function is called. However, the main () function is not directly called, but is automatically executed when the program is running. So how does the main () function obtain parameters? The method is obtained from the command line.
Here we need to use the command line variable WM_KILLFOCUS, d, and I to start the grep program. We want to demonstrate how to change it into argc and argv in the main () function. first, the argc variable contains at least 1 Number of parameters passed in the command line, because the program name is also counted as a parameter. The variable argv is an array containing the pointer of a string. This array contains each string passed in the command line. In this example:
- 1: #include <iostream.h>
-
- 2: #include <conio.h>
-
- 3: #pragma hdrstop
-
- 4:
-
- 5: int main(int argc,char **argv)
-
- 6: {
-
- 7:cout << "argv = "argc << end1;
-
- 8.for (int i=0;i<argc;i++)
-
- 9. cout << "Parameter " << i << ": " << argv[i]<< end1;
-
- 10. cout << end1 << "Press any key to continue...";
-
- 11: getch();
-
- 12: return 0;
-
- 13: }
Save the Project as arstest. Instead of clicking the Run button, select Project | Build All in the main menu. In this way, only the Project is created and no program is executed. After the project is completed, select Run | Parameters in the main menu and enter the following content in the RunParameters field in the RunParameters dialog box: one two three "four five" six and then click the Run button, the program runs with the specified command line parameters.
Another method is to run the program at the DOS prompt using the following command line: When arstest one two three "four five" six is running, it displays the number of incoming changes, then list each variable. Run the command several times and provide variable elements for different command lines each time. Pay attention to the result.
A powerful feature of C ++ functions is direct access to memory. Due to this feature, the C ++ function cannot prevent you from writing to a specific memory address, even if the address is not accessible by the program. The following code is valid, but may cause program or Windows to crash: int array [5]; array [5] = 10; this is a common error because the array is based on 0, the biggest goal should be 4 rather than 5.
If the end of the array is reloaded, you cannot know which memory is rewritten, making the results unpredictable, and even causing program or Windows crash. This type of problem is difficult to diagnose, because the affected memory usually takes a long time to access, and then the crash occurs, which makes you confused ). Therefore, be careful when writing arrays.