C Language Learning tutorial sixth chapter-pointers (7)

Source: Internet
Author: User
Tags command line dbase header integer variables printf

parameter of main function

The main function described earlier is all with no arguments. Therefore, the parentheses after main are empty parentheses. In fact, the main function can take parameters, which can be considered as the formal parameters of the main function. The C language stipulates that there can be only two parameters for the main function, which are customarily written as argc and argv. Therefore, the function header of the main function can be written as: Main (ARGC,ARGV) C also stipulates that ARGC (the first parameter) must be an integer variable, argv (the second parameter) must be an array of pointers to the string. With the formal parameter description, the function header of the main function should be written as:
Main (ARGC,ARGV)
int argv;
Char *argv[]; or written as:
Main (int Argc,char *argv[])
Because the main function cannot be called by another function, it is not possible to get the actual value inside the program. So where do you give the argument value to the formal parameter of the main function? In fact, the parameter values of the main function are obtained from the operating system command line. When we want to run an executable file, type the filename at the DOS prompt, and then enter the actual parameters to transfer the arguments to the main parameter.

The general form of the command line at the

dos prompt is: c:\> executable filename parameter parameters ...; however, it should be noted that the two parameters in main and the arguments in the command line are not one by one corresponding on the
position. Because there are only two formal parameters for main, the number of arguments in the command line is not limited in principle. The ARGC parameter represents the number of arguments in the command line (note: The filename itself is also a parameter), and the value of the ARGC is automatically given by the system by the number of actual arguments when the command row is entered. For example, there is a command behavior: C:\>e6 BASIC dbase Fortran because the filename E6 24 itself is also counted as a parameter, so there are 4 parameters, so argc gets a value of 4. The argv parameter is an array of string pointers whose element values are the first address of each string in the command line (the parameters are processed by string). The length of the pointer array is the number of parameters. The initial value of an array element is automatically assigned by the system. The representation is shown in Figure 6.8:
Main (int Argc,char *argv) {
while (argc-->1)
printf ("%s\n", *++ARGV);
}
This example displays the parameters entered in the command line, if the executable file named E24.exe, is stored on the disk of a drive.
Therefore enter the command behavior: C:\>a:e24 basic DBASE FORTRAN
Runs the result:
BASIC
DBASE
Fortran
The line has 4 parameters, and when you execute main, The initial value of the ARGC is 4. The 4 elements of the argv are divided into the first addresses of 4 strings. Executes the while statement, argv value minus 1 per loop, and argv equals 1 o'clock to stop the loop, looping three times, so you can output three parameters altogether. In the printf function, because the print item *++ARGV is 1 and then printed, the first time you print is the string basic that argv[1] refers to. The second to third cycle prints the following two strings respectively. The parameter E24 is the filename and does not have to be exported.

The following example has two parameters in the command line, and the second parameter 20 is the input n value. The value that is *++ARGV in the program is the string "20", and then the function "Atoi" is used to replace the integer as the loop control variable in the while statement, and the output is 20 even.
#include "Stdlib.h"
Main (int argc,char*argv[]) {
int a=0,n;
N=atoi (*++ARGV);
while (n--) printf ("%d", a++*2);
}
This program outputs n even numbers starting from 0. Pointer variable to pointer if a pointer variable holds the address of another pointer variable, it is called a pointer variable that points to the pointer.

In the previous introduction, the pointer access variable is called indirect access, referred to as an interview. Because pointer variables are directed to variables, they are called single level visits. And if the variable is accessed by pointing to the pointer variable, it constitutes a level two or multi-level visit. In the C language program, there is no explicit limit to the number of visits, but the number of visits is too long to understand the solution and error prone, therefore, generally rarely more than two visits. The general form of the description of the pointer variable pointing to the pointer is:
Type descriptor * * pointer variable name;
For example: int * * PP; Indicates that PP is a pointer variable that points to another pointer variable, and the pointer variable points to an integer quantity. Here is an example to illustrate this relationship.
Main () {
int x,*p,**pp;
x=10;
p=&x;
pp=&p;
printf ("x=%d\n", **pp);
}
In the example program, p is a pointer variable, and the integer x;pp is also a pointer variable, which points to the pointer variable p. The writing of Access x through PP variable is **pp. The value of the program's last output X is 10. In the above example, the reader can learn the description and usage of pointer variables pointing to pointers.

The following program first defines the pointer array ps and initializes the assignment. It also shows that PPS is a pointer variable pointing to a pointer. In 5 cycles, PPS obtained the address value of ps[0],ps[1],ps[2],ps[3],ps[4] respectively (as shown in Figure 6.10). The string can then be found by these addresses.
Main () {
static char *ps[]={"BASIC", "DBASE", "C", "FORTRAN",
"PASCAL"};
Char **pps;
int i;
for (i=0;i<5;i++) {
Pps=ps+i;
printf ("%s\n", *pps);
}
}
This program is a pointer to the pointer variable programming, output multiple strings.

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.