【发布时间】:2020-05-19 15:35:17
【问题描述】:
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;
}
【问题讨论】:
-
请正确缩进。
-
如果我说:pleaseindentthecode,不像我在上面加空格那么容易阅读,对吧?
-
看起来像数组的函数参数实际上是指针。您可以增加指针;您不能增加不是函数参数的数组名称。您使用的是哪个版本的 K&R?两者都有点旧,但您不应该使用第一版(1978 年)。
-
你好,很抱歉没有很好地呈现代码,但它不是代码的逻辑或我所指的代码的工作方式,我的问题很简单,我们如何使用增量数组名?
-
@FiddlingBits 我实际上是在准备一场竞争性考试,这是 C 考试中最推荐的书,所以不得不接受它..
标签: c arrays pointers command-line main