为什么要检查 if (*argv == NULL)?

发布于 2024-08-23 03:49:54 字数 867 浏览 14 评论 0原文

在我目前正在学习的数据结构课程中,我们的任务是用 C++ 编写一个网络爬虫。为了让我们抢占先机,教授为我们提供了一个从给定 URL 获取源代码的程序,以及一个简单的 HTML 解析器来剥离标签。该程序的 main 函数接受参数,因此使用 argc/argv。用于检查参数的代码如下:

// Process the arguments
if (!strcmp(option, "-h"))
{
    // do stuff...
}
else if (!strcmp(option, ""))
{
    // do stuff...
}
else if (!strcmp(option, "-t"))
{
    // do stuff...
}
else if (!strcmp(option, "-a"))
{
    // do stuff...
}

if ( *argv == NULL )
{
    exit(1);
}

其中“option”已使用 argv[1] 中的开关填充,argv[2] 及更高版本具有剩余参数。我理解的第一个块很好,如果开关等于字符串,则根据开关执行任何操作。我想知道最后一个 if 块的目的是什么。

可能是我的 C++ 有点生疏,但我似乎记得 *argv 相当于 argv[0],基本上意味着它正在检查以确保参数存在。但我的印象是 argv[0] 总是(至少在大多数实现中)包含正在运行的程序的名称。我发现如果 argc 等于 0,argv[0] 可能为 null,但是在 Google 上搜索我找不到任何帖子来确定这是否可能。

所以我转向你。最终的 if 块检查到底是什么?

编辑:我已经接受了所选答案的注释中提供的推理,即可能故意导致 argv[0] 变为 NULL,或者基于平台变为 NULL - main的具体实现。

In the data structures class that I am currently taking, we have been tasked with writing a web crawler in C++. To give us a head start, the professor provided us with a program to get the source from a given URL and a simple HTML parser to strip the tags out. The main function for this program accepts arguments and so uses argc/argv. The code used to check for the arguments is as follows:

// Process the arguments
if (!strcmp(option, "-h"))
{
    // do stuff...
}
else if (!strcmp(option, ""))
{
    // do stuff...
}
else if (!strcmp(option, "-t"))
{
    // do stuff...
}
else if (!strcmp(option, "-a"))
{
    // do stuff...
}

if ( *argv == NULL )
{
    exit(1);
}

Where "option" has been populated with the switch in argv[1], and argv[2] and higher has the remaining arguments. The first block I understand just fine, if the switch equals the string do whatever based on the switch. I'm wondering what the purpose of the last if block is though.

It could be that my C++ is somewhat rusty, but I seem to recall *argv being equivalent to argv[0], basically meaning it is checking to make sure arguments exist. Except I was under the impression that argv[0] always (at least in most implementations) contained the name of the program being run. It occurs to me that argv[0] could be null if argc is equal to 0, but searching around on Google I couldn't find a single post determining whether or not that is even possible.

And so I turn to you. What exactly is that final if block checking?

EDIT: I've gone with the reasoning provided in the comments of the selected answer, that it may be possible to intentionally cause argv[0] to become NULL, or otherwise become NULL based on an platform-specific implementation of main.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅紫色的梦幻 2024-08-30 03:49:54

3.6.1/2:

如果 argc 非零这些参数
应在 argv[0] 中提供
...并且 argv[0] 应该是指针
NTMBS 的初始字符
代表所用的名称
调用程序或“”。的价值
argc 应为非负数。价值
argv[argc] 的值应为 0。

强调是我的。 argc 只保证非负数,而不保证非零。

这是 main 的入口。 //do stuff 也有可能修改 argv 的值或其指向的数组的内容。选项处理代码在处理 argv 时将值移出的情况并非完全闻所未闻。因此,对 *argv == null 的测试可能是在删除或跳过选项后测试是否还剩下任何命令行参数。您必须查看其余的代码。

3.6.1/2:

If argc is non-zero those arguments
shall be provided in argv[0] though
... and argv[0] shall be the pointer
to the initial character of a NTMBS
that represents the name used to
invoke the program or "". The value of
argc shall be nonnegative. The value
of argv[argc] shall be 0.

Emphasis mine. argc is only guaranteed non-negative, not non-zero.

This is at entry to main. It's also possible that //do stuff modifies the value of argv, or the contents of the array it points to. It's not entirely unheard of for option-handling code to shift values off argv as it processes them. The test for *argv == null may therefore be testing whether or not there are any command-line arguments left, after the options have been removed or skipped over. You'd have to look at the rest of the code.

笑着哭最痛 2024-08-30 03:49:54

argc 将为您提供传递的命令行参数的数量。您不需要检查 argv 的内容来查看是否没有足够的参数。

if (argc <= 1) { // The first arg will be the executable name
   // print usage
}

argc will provide you with the number of command line arguments passed. You shouldn't need to check the contents of argv too see if there are not enough arguments.

if (argc <= 1) { // The first arg will be the executable name
   // print usage
}
孤独陪着我 2024-08-30 03:49:54

请记住 C 的可移植性,它可能并不总是在 Windows 或 Unix 等标准平台上运行。也许它是洗衣机内的一些微代码,运行在一些廉价的、被黑客攻击的环境中。因此,在取消引用指针之前确保指针不为空是一个很好的做法,这可能会导致问题。

即便如此,你还是对的。 *argv 与 argv[0] 相同,并且 argv 应该由环境初始化(如果提供的话)。

Remembering just how portable C is, it might not always be running on a standard platform like Windows or Unix. Perhaps it's some micro-code inside your washing machine running on some cheap, hacked environment. As such, it's good practice to make sure a pointer isn't null before dereferencing it, which might have led to the question.

Even so, you're correct. *argv is the same as argv[0], and argv is supposed to be initialized by the environment, if it's provided.

杯别 2024-08-30 03:49:54

只是一个猜测。

如果你的教授指的是这个怎么办?

while(*++argv !=NULL)

    printf("%s\n",*argv);

just a speculation.

what if your professor is referring to this ??

while(*++argv !=NULL)

    printf("%s\n",*argv);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文