命令行函数理解问题(*++argv)[0] 和 *++argv[0] )
while (--argc > 0 && (*++argv)[0] == '-')while( c = *++argv[0])
(*++argv)[0] == argv[1][0] == *(argv + 1) [0] 就是第二行首字符
*++argv[0] == argv[0][1] == *(argv[0] + 1) 就是第一行第二列。就是这个指针纠结了很久。貌似我这样理解程序就是错误的,大牛帮忙指点下
程序代码:
#include<stdio.h> #include<string.h> #define MAXLINE 1000 int getline(char *s, int max); int main(int argc, char *argv[]) { char line[MAXLINE]; long lineno = 0; int c, except = 0, number = 0, found = 0; int i = 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 : illgal 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; }