对 argc、argv 及其用法的有趣观察

发布于 2025-01-02 23:21:55 字数 992 浏览 2 评论 0原文

因此,我正在开发一个基于 Linux 的命令行实用程序,它必须接受一些标志,并且我注意到一些有趣的行为。我将发布我在主实用程序之外使用的测试代码。我正在使用这段代码,所以我不必改变实际的实用程序,直到我有了可以插入的工作代码。所以这是我一直在摆弄的代码:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    while(--argc && (*++argv)[0] == '-') 
        putchar('*');

    printf("\n%s\n", argv[0]);
 }   

请忽略这个程序所做的所有事情就是打印一个星号并在使用一个参数 -b 调用时打印它自己的名称。打印星号只是为了表明循环运行了一次。因此,我在终端中以“./test -n”的形式运行此命令,我期望输出为:

*./test

令我惊讶的是,输出为:

*-b

我对该语句有一个工作理论 (*++argv)[0] 正在做,但我仍然有点模糊。我的假设是,它遍历指针数组,查看指向的每个字符串中的第一个字符, (*++argv)[0] 现在取消引用 *argv[0]< /code> 或第一个参数字符串的元素零。

所以,基本上我有三个问题:

  1. 该声明到底在做什么?
  2. 为什么无论我如何尝试都无法返回 argv[0] 或 argv[0][0]?
  3. 是否将 argv[0] 指向的地址处的值存储在另一个 char * 中,这是我此时能够访问该值的唯一方法,这是解决此问题的正常方法吗?

我现在对此感到非常困惑,并且已经尝试了我能想到的一切来解决这个问题。有一次我有一个循环可以打印字母表,我不知道程序正在访问内存的哪一部分。最有趣的排列是从某处提取 sshid 变量。

预先感谢大家对此的帮助。

So, I am working on a Linux based command line utility, that has to accept a few flags, and I have noticed some interesting behavior. I will be posting testing code that I was using outside of the main utility. I was using this code, so I did not have to alter the actual utility until I had working code that I could just insert. So here is the code that I have been fiddling with:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    while(--argc && (*++argv)[0] == '-') 
        putchar('*');

    printf("\n%s\n", argv[0]);
 }   

Please ignore the fact that all this program does is print an asterisk and print its own name when invoked with one argument -b. The printing of the asterik was just to show that the loop ran once. So, I run this in a terminal as "./test -n", and I expected the output to be:

*./test

Much to my surprise, the output was:

*-b

I have a working theory of what the statement
(*++argv)[0] is doing, but I am still a little hazy on it. My assumption is that it steps across the array of pointers looking at the first character in each string pointed to, (*++argv)[0] is now dereferencing *argv[0] or element zero of the first argument string.

So, basically I have three questions:

  1. What exactly is that statement doing?
  2. Why can I not get back to argv[0] or argv[0][0], no matter what I try?
  3. Is storing the value at the address pointed to by argv[0] in another char *, this is the only way that I have been able to access that value at this point, the normal way around this?

I am really confounded by this at the moment and have tried everything that I can think of to work this out. At one point I had a loop that would print the alphabet, I don't know what part of memory the program was accessing. The most interesting permutation was pulling sshid variables from somewhere.

Thank you all in advance for your help with this.

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

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

发布评论

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

评论(1

烟酉 2025-01-09 23:21:55

++argv 更改 argv 以指向下一个参数。

尝试类似

int i = 0;
while(--argc && argv[++i][0] == '-')

Which 维护单独的索引,而不是覆盖 argv 。

或者

char** argp = argv;
while(--argc && (*++argp)[0] == '-')

它的工作方式与原始版本相同,只是它更改了 argv 的副本而不是原始版本。

++argv changes argv to point to the next argument.

Try something like

int i = 0;
while(--argc && argv[++i][0] == '-')

Which maintains a separate index, instead of overwriting argv.

Or

char** argp = argv;
while(--argc && (*++argp)[0] == '-')

which works the same as the original, except it changes a copy of argv instead of the original.

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