Jas*_*son 0 c unix command-line-arguments
我的教授在课堂上引用了这个例子.它基本上是Unix more
命令的一个版本,我不确定其中的几个东西
int main( int ac , char *av[] )
{
FILE *fp;
if ( ac == 1 )
do_more( stdin );
else
while ( --ac )
if ( (fp = fopen( *++av , "r" )) != NULL )
{
do_more( fp ) ;
fclose( fp );
}
else
exit(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我理解*fp
定义了一个文件指针,而*av []是命令行参数的数组.但*++av
在操作方面意味着什么呢?
阅读*++ av像这样:
++av // increment the pointer
*av // get the value at the pointer, which will be a char*
Run Code Online (Sandbox Code Playgroud)
在此示例中,它将打开命令行上传递的每个文件.
也:
av[0] // program name
av[1] // parameter 1
av[2] // parameter 2
av[3] // parameter 3
av[ac - 1] // last parameter
Run Code Online (Sandbox Code Playgroud)