开发者

Using spiral rule in command line argument

开发者 https://www.devze.com 2023-04-05 23:43 出处:网络
what is the difference between the below declarations? char *argv[]; and char *(argv[]); I th开发者_开发百科ink it is same according to spiral rule.As written, the parentheses make no difference

what is the difference between the below declarations?

char *argv[];

and

char *(argv[]);

I th开发者_开发百科ink it is same according to spiral rule.


As written, the parentheses make no difference.

The so-called spiral rule falls out of this simple fact of C grammar: postfix operators such as () and [] have higher precedence than unary operators like *, so expressions like *f() and *a[] are parsed as *(f()) and *(a[]).

So given a relatively complex expression like

*(*(*foo)())[N]

it parses as

     foo            -- foo
   (*foo)           -- is a pointer (parens force grouping)
   (*foo)()         -- to a function
 (*(*foo)())        -- returning a pointer (parens force grouping again)
 (*(*foo)())[N]     -- to an array
*(*(*foo)())[N]     -- of pointer


Yes, they are the same. char *(argv[]) still means an array of pointers.

char (*argv)[] would be different as it means a pointer to an array of char's.


  1. argv[] is not a type so (argv[]) can't be a function declaration - it's a precedence operation.
  2. Using the spiral rule we first find [] (precedence or not) and then *, just as we do with *argv[], thus they are equal.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号