间差(* ++的argv)[0]和同时(C = * ++的argv [0]) [英] Difference between (*++argv)[0] and while(c = *++argv[0])

查看:144
本文介绍了间差(* ++的argv)[0]和同时(C = * ++的argv [0])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code以下片段:

I have the following snippet of code:

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] == '-') //These two lines
        while(c = *++argv[0])                 //These two lines
          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;
          }

     ...
}


含有下述前pressions:


Containing the following expressions:

while(--argc > 0 && (*++argv)[0] == '-')

在是否括号这个前pression (* argv的++)[0] 不同而(C = * ++的argv [0])没有括号?

如果是这样,如何​​?请问(* argv的++)的意思是指向下一个参数,它 * argv的++ [0] 意思指针到当前字符阵列中的下一个字符正在被指向的

If so, how? Does (*++argv) mean pointer to the next argument, and does *++argv[0] mean pointer to the next character in the current char array which is being pointed to?

推荐答案

首先,K&安培; R对这个特殊的代码片段的勘误:

First, K&R have an errata on this particular snippet:

117(§5.10):在找到的例子,该计划增量的argv [0] 。这不是明令禁止,但是未明确允许任何

117(§5.10): In the find example, the program increments argv[0]. This is not specifically forbidden, but not specifically allowed either.

现在的解释。

假设你的程序被命名为 PROG ,和你执行它: PROG -AB -c的Hello World 。你想成为能够分析的参数说,选择 A B Ç 中指定,而您好全球都是非选项参数。

Let's say your program is named prog, and you execute it with: prog -ab -c Hello World. You want to be able to parse the arguments to say that options a, b and c were specified, and Hello and World are the non-option arguments.

的argv 的类型为的char ** —请记住,在函数数组参数是一样的一个指针。在程序调用,事情是这样的:

argv is of type char **—remember that an array parameter in a function is the same as a pointer. At program invocation, things look like this:

                 +---+         +---+---+---+---+---+
 argv ---------->| 0 |-------->| p | r | o | g | 0 |
                 +---+         +---+---+---+---+---+
                 | 1 |-------->| - | a | b | 0 |
                 +---+         +---+---+---+---+
                 | 2 |-------->| - | c | 0 |
                 +---+         +---+---+---+---+---+---+
                 | 3 |-------->| H | e | l | l | o | 0 |
                 +---+         +---+---+---+---+---+---+
                 | 4 |-------->| W | o | r | l | d | 0 |
                 +---+         +---+---+---+---+---+---+
                 | 5 |-------->NULL
                 +---+

在这里, ARGC 5和 ARGV [ARGC] NULL 。在开始的时候,的argv [0] 是包含字符串PROG一个的char *

Here, argc is 5, and argv[argc] is NULL. At the beginning, argv[0] is a char * containing the string "prog".

(* argv的++)[0] ,因为括号中,的argv 先增加,然后取消引用。增量的作用是移动的的argv ----------> 箭头一挡了下来,指向 1 。提领的作用是获得一个指向第一个命令行参数, -AB 。最后,我们采取的第一个字符( [0] (* argv的++)[0] )的本串,并对其进行测试,看它是否是 - ,因为这表示一个选项开头

In (*++argv)[0], because of the parentheses, argv is incremented first, and then dereferenced. The effect of the increment is to move that argv ----------> arrow "one block down", to point to the 1. The effect of dereferencing is to get a pointer to the first commandline argument, -ab. Finally, we take the first character ([0] in (*++argv)[0]) of this string, and test it to see if it is '-', because that denotes the start of an option.

对于第二个结构中,我们真正想要走在串的电流的argv [0] 指针指向。所以,我们需要把的argv [0] 为指针,忽视了它的第一个字符(即 - 因为我们刚刚测试),并期待在其他字符:

For the second construct, we actually want to walk down the string pointed to by the current argv[0] pointer. So, we need to treat argv[0] as a pointer, ignore its first character (that is '-' as we just tested), and look at the other characters:

++(的argv [0])将递增的argv [0] ,得到一个指针第一个非 - 字符,并取消引用它会给我们该字符的值。所以,我们得到 * ++(的argv [0])。但是,由于在C, [] 结合更加紧密比 ++ ,我们实际上可以摆脱括号,并得到我们的前pression为 * argv的++ [0] 。我们要继续处理这个角色,直到它的 0 (最后一个字符框在每个在上面图片中的行数)。

++(argv[0]) will increment argv[0], to get a pointer to the first non- - character, and dereferencing it will give us the value of that character. So we get *++(argv[0]). But since in C, [] binds more tightly than ++, we can actually get rid of the parentheses and get our expression as *++argv[0]. We want to continue processing this character until it's 0 (the last character box in each of the rows in the above picture).

这位前pression

The expression

c = *++argv[0]

分配给 C 当前选项的值,而值为 C 而(C)而速记(C!= 0),所以而(C = * argv的++ [0])线基本上是当前选项的值赋给 C 和测试,看是否我们已经达到了目前的命令行参数的结尾。

assigns to c the value of the current option, and has the value c. while(c) is a shorthand for while(c != 0), so the while(c = *++argv[0]) line is basically assigning the value of the current option to c and testing it to see if we have reached the end of the current command-line argument.

在这个循环结束时,ARGV将指向第一个非选项参数:

At the end of this loop, argv will point to the first non-option argument:

                 +---+         +---+---+---+---+---+
                 | 0 |-------->| p | r | o | g | 0 |
                 +---+         +---+---+---+---+---+
                 | 1 |-------->| - | a | b | 0 |
                 +---+         +---+---+---+---+
                 | 2 |-------->| - | c | 0 |
                 +---+         +---+---+---+---+---+---+
 argv ---------->| 3 |-------->| H | e | l | l | o | 0 |
                 +---+         +---+---+---+---+---+---+
                 | 4 |-------->| W | o | r | l | d | 0 |
                 +---+         +---+---+---+---+---+---+
                 | 5 |-------->NULL
                 +---+

这是否帮助?

这篇关于间差(* ++的argv)[0]和同时(C = * ++的argv [0])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆