Using spiral rule in command line argument
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.
argv[]
is not a type so(argv[])
can't be a function declaration - it's a precedence operation.- Using the spiral rule we first find
[]
(precedence or not) and then*
, just as we do with*argv[]
, thus they are equal.
精彩评论