[英]Why does this program in Dennis Ritchie's "The C Programming Language" not work?
[英]How is "++argv" correct in this program from "The C programming Language by Dennis Ritchie"?
main 函數有第二個參數char * argv[] ,這是一個指針數組。在同一本書中已經提到我們不能在數組名稱上使用“++”之類的操作。 但是,在這里我們可以看到 ++argv 已被使用。
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
int getline(char* line, int max);
/* find: print lines that match pattern from 1st arg */
int main(int argc, char* argv[])
{
char line[MAXLINE];
long lineno = 0;
int c, except = 0, number = 0, found = 0;
while (--argc > 0 && (*++argv)[0] == '-')
{
while (c = *++argv[0])
{
switch (c)
{
case 'x': {
except = 1;
}
break;
case 'n': {
number = 1;
}
break;
default: {
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
}
break;
}
}
}
if (argc != 1)
{
printf("Usage: find -x -n pattern\n");
}
else
{
while (getline(line, MAXLINE) > 0)
{
lineno++;
if ((strstr(line, *argv) != NULL) != except)
{
if (number)
{
printf("%ld:", lineno);
}
printf("%s", line);
found++;
}
}
}
return found;
}
作為函數參數的數組會自動轉換為指針。 所以這:
char *argv[]
完全一樣:
char **argv
這就是為什么你被允許做argv++
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.